-1

I'm trying to make a AJAX request on my theme.

I already know wordpress handle AJAX very well, but not directly on theme file. It uses admin-ajax.php for this.

I working on this for a long time and really don't know where to put the code.

I know there need to put information in admin-ajax.php, function.php and have to specify if user is ou not logged in.

Can someone help me with my code?

HTML:

<article class="delivery-individual">
        <!-- # CONTENT HERE -->
</article>

JQUERY:

    <script>
$.ajax({
      url: "<?php bloginfo('template_directory'); ?>/temakis.php",
      dataType: "html",
      success: function(result) {
        $(".delivery-individual").html(result);
      }
    });
</script>

EXTERNAL FILE:

<h3>Temakis</h3>

<?php
$args = array(
    'post_type'      => 'post',
    'category_name' => 'temakis');
query_posts($args);
while (have_posts()) : the_post();
?>

<div class="item-individual">
  <img src="<?php the_field("imagem-produto");?>" width="150" height="150" title="<?php echo get_the_title(); ?> "/>
  <div class="item-individual-info">
  <h4><?php echo get_the_title(); ?></h4>
  <p class="descricao-delivery"><? echo get_post_field('post_content'); ?></p>
  <p class="valor-delivery">R$ <? the_field("preco"); ?></p>
  </div>
</div>

<?php endwhile; ?>
<?php wp_reset_query(); ?>

The request is ok, because i`m getting the h3 correctly.

Can anyone help me?

Thanks!

4

2 Answers 2

0

You need to set user logged in condition on button/ link which triggers ajax request, eg, if user is not logged in open login modal else your function.

0

It doesn't work because your external file is not loaded under wordpress, and therefore doesn't have access to any wordpress functions.

As soon as your external file hits query_posts() it will throw a fatal error as the function is not defined.

You should use the Ajax API as has been suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.