javascript - AJAX 请求重复项目

标签 javascript php jquery ajax wordpress

我正在使用 AJAX 来“显示更多”。目前我的自定义帖子类型最初显示 12,当单击“显示更多”按钮时,页面上加载了另外 12 个 post-types

问题:第一次点击后,显示了 12 个。但是每次点击继续,它会继续重复之前填充的 12 个项目。

问题:当用户点击“显示更多”时,如何正确使用 AJAX 以 12 秒显示帖子。

Functions.php 中的 AJAX 处理程序

function ajax_more_team($offset) {

  $offset = $offset + 12;
  header("Content-Type: text/html");

  $args = array(
      'suppress_filters' => true,
      'post_type'   =>  'team',
      'posts_per_page'  =>  12,
      'offset'  =>  $offset,
  );

  $the_query = new WP_Query($args);

     if ($the_query -> have_posts()) :  while ($the_query -> have_posts()) : $the_query -> the_post();

          //Loop content            

        endwhile;
     endif;
  wp_reset_postdata();
  die($out);
}

Jquery函数

var count = 0;

function load_more_team(count) {

    var count = count + 12

    var button = $('#more_posts'),
        data =  {
            'action': 'ajax_more_team',
            'offset': count
        }

    $.ajax({
        url: team_ajax.ajaxurl,
        data: data,
        type: 'POST',
        dataType: 'html',
        success: function(data){
            if(data.length){
                $("#ajax_posts").append(data);
                button.attr("disabled",false);

            } else{
                button.attr("disabled",true);
            }
        }
    });
    return false;
}

$('#more_posts').click(function() {
    $("#more_posts").attr("disabled",true); // Disable the button, temp.
    load_more_team();
});

编辑:

为了添加一些额外的上下文,我添加了初始页面模板循环。

page.php

<div id="ajax_posts" class="row">

        <?php
            $args = array(
                'post_type' =>  'team',
                'posts_per_page'    =>  12
            );
            $the_query = new WP_Query($args);
        ?>

        <?php if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
            <?php $id = get_the_id(); ?>
            <div class="col-6 col-md-4 col-lg-3 col-xl-2 mb-4">
                <div class="team-member">
                    <a href="#" data-toggle="modal" data-target="#<?php echo $id ?>">
                        <img class="img-fluid" src="<?php the_field('employee_headshot'); ?>" alt="<?php the_field('employee_name'); ?>">
                    </a>
                    <div class="team-info">
                        <h6><?php the_field('employee_name'); ?></h6>
                    </div>
                    <a href="" data-toggle="modal" data-target="#myModal">
                        <div class="modal-icon">
                            <img class="img-fluid" src="<?php bloginfo('template_directory');?>/imgs/modal-icon.svg">
                        </div>
                    </a>
                </div>
                <!-- Modal -->
                <div class="modal fade" id="<?php echo $id ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="team-close-btn">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <img class="img-fluid" src="<?php the_field('employee_headshot'); ?>" alt="Alice George">
                                <div class="team-info">
                                    <h6><?php the_field('employee_name'); ?></h6>
                                    <p><strong>Title:<br></strong> <?php the_field('employee_title'); ?></p>
                                    <p><strong>Company:<br></strong> <?php the_field('employee_company'); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php endwhile; endif; ?>
    </div>
    <?php 
        if( $the_query->max_num_pages > 1)
        echo '<div id="more_posts" class="btn-primary mt-4">View More</div>'
    ?>
    <?php wp_reset_postdata(); ?>
</div>

最佳答案

更新到您的代码,这应该适合您。我认为计数有点困惑,因为对我来说,它不是立即清楚是总数还是当前页面。

不确定您是如何在 PHP 函数中接收 $offset 值的,但是此处的 $offset 应该只是 POST 值“offset”,而不是 + 12 或其他任何值。这样,第一次调用时,您的偏移量为 0 并从第 1 行开始获取前 12 条记录,下次您的偏移量为 12 并获取接下来的 12 条记录(从第 13 行开始)。您的 post_per_page 保持 12,而 offset 成倍增加。偏移量表示它应该从哪些记录开始获取“post_per_page”数量。

function ajax_more_team($offset) {

  $offset = $_POST['offset'];
  header("Content-Type: text/html");

  $args = array(
      'suppress_filters' => true,
      'post_type'   =>  'team',
      'posts_per_page'  =>  12,
      'offset'  =>  $offset,
  );

  $the_query = new WP_Query($args);

     if ($the_query -> have_posts()) :  while ($the_query -> have_posts()) : $the_query -> the_post();

          //Loop content            

        endwhile;
     endif;
  wp_reset_postdata();
  die($out);
}

-

var page = 1; // first page

function load_more_team() {


var button = $('#more_posts'),
    data =  {
        'action': 'ajax_more_team',
        'offset': page * 12 // first time 0 * 12 = offset 0
    }

$.ajax({
    url: team_ajax.ajaxurl,
    data: data,
    type: 'POST',
    dataType: 'html',
    success: function(data){
        if(data.length){
            $("#ajax_posts").append(data);
            button.attr("disabled",false);

        } else{
            button.attr("disabled",true);
        }
        page++; // increment page after first request
    }


});


return false;
}

关于javascript - AJAX 请求重复项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175893/

相关文章:

javascript - 如何调整 table 内 table 的高度

javascript - ES6 避免那个/自己

php - 计算 MySQL 列中的总值并在 PHP 中检索值

php - 搜索字符串形成数据库表中的多列

php - 如何使用 Omnipay 退款

jquery - 如何展开文本鼠标悬停 - css3, jQuery, JavaScript

javascript - knockout.js 阻止第一个值更改

javascript - 从 JSON 属性加载 HTML img 元素的图像,而 URL 不能直接访问

javascript - 点击事件并不总是触发

javascript - 使用 SetInterval 更改 CSS 背景 url