it_proger29
@it_proger29
Битрикс

Не работает ajax запрос в wp, что делать?

function.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; ?>


Как бы всё работает, но проблема в том, что выводятся все посты 5 рубрики.
5c58ad1f668f7031020682.png
  • Вопрос задан
  • 257 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы