V
V
Vladimir Kulikov2019-02-05 00:22:43
WordPress
Vladimir Kulikov, 2019-02-05 00:22:43

Ajax request not working in wp, what should I do?

functions.php

add_action('wp_ajax_my_action', 'data_fetch');
add_action('wp_ajax_nopriv_my_action', 'data_fetch');

function data_fetch($postID){
    $postID=intval( $_POST['param'] );
    
    $args = array(
    'p' => $postID, // ID of a page, post, or custom type
    //’post_type’ => ‘any’
    'cat' => 5,
    );
    $recent = new WP_Query($args);
    while ( $recent->have_posts() ) : $recent->the_post();?>
        <div class="row">
            <div class="col-md-12">
                <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="" style="width: 100%;">
            </div>
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel"><?php the_title( '<h2 class="modal-title text-center">', '</h2>' ); ?></h4>
            </div>
            <div class="modal-header">
                <p><?php the_content(); ?></p>
            </div>
        </div>
    <?php endwhile; ?>
<?php
    die();
}

index.php
<script>
$( '[data-ajax-param]' ).click(function (e) {
 fetch(e);
});
function fetch(e){var param = $(e.target).attr('data-ajax-param');
 // Находим id поста по нажатию кнопки. У кнопки должен быть атрибут data-ajax-param равный id поста, например, data-ajax-param="11"
  $.post('wp-admin/admin-ajax.php', {'action':'my_action', 'param':param}, function(response){
   $('.modal-content.ajax').html(response);
});
}
</script>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content ajax">
        <div class="row ">
            <div class="col-md-12">
                <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="" style="width: 100%;">
            </div>
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel"><?php the_title( '<h2 class="modal-title text-center">', '</h2>' ); ?></h4>
            </div>
            <div class="modal-header">
                <p><?php the_content(); ?></p>
            </div>
        </div>
      <div class="modal-body">
      </div>
    </div>
  </div>
</div> 

// Вывожу записи. По нажатию тут записывается в data-ajax-param ид поста.
     <?php
      $args = array(
'p' => $postID, // ID поста
   'posts_per_page' => 4,
    'cat' => 5,
'post_type' => 'any'
);
$recent = new WP_Query($args);
while ( $recent->have_posts() ) : $recent->the_post();?>
        <div class="col-md-3 portf">
           <a href="#openModal" class="" data-toggle="modal" data-ajax-param="<?php the_ID(); ?>" data-target="#myModal">
            <img src="<?php the_post_thumbnail_url() ?>" style="object-fit: cover;width: 200px;height: 200px;" alt="">
            </a>
        </div>

<?php endwhile; ?>

As if everything works, but the problem is that all posts of the 5th heading are displayed.
5c58ad1f668f7031020682.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question