python小菜 2017-07-18 19:53 采纳率: 0%
浏览 26

AJAX请求重复项

I'm using AJAX to "Show More". Currently I have my custom post type show 12 initially and when the "Show More" button is clicked, 12 more post-types are loaded on page.

The Issue: After the first click, 12 are shown. But every click proceeding, it continues to repeat the previously populated 12 items.

Question: How can I properly use AJAX to show the posts, by 12s, when the users clicks "Show More".

AJAX Handler in Functions.php

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 Function

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();
});

Edit:

To add a bit of extra context, I am adding the initial page template loop.

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>
  • 写回答

2条回答 默认 最新

  • weixin_33749242 2017-07-18 19:57
    关注

    Updated to your code, this should work for you. I think count was a bit confusing, cause for me, it wasn't immediatly clear if it was a total amount or the current page.

    Not sure how you're recieving the $offset value in your PHP function but $offset here should be just the POST value 'offset', no + 12 or anything. That way at first call you have a offset of 0 and get first 12 records starting at row 1, next time you have offset of 12 and get next 12 records (starting at the 13th row). Your post_per_page remain 12, while offset multiplies. Offset indicates from what records it should begin taking the "post_per_page" amount.

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能