简体   繁体   English

WordPress循环耗尽帖子?

[英]Wordpress loop running out of posts?

I'm confused as to what is happening here, I have 11 posts, with 3 initially loaded and 3 that load every time I scroll to the bottom of the page until all 11 are shown. 我对这里发生的事情感到困惑,我有11个帖子,每次我滚动到页面底部直到显示全部11个帖子时,最初加载了3个帖子,并加载了3个帖子。 This is fine. 这可以。

The problem occurs when I click a button and dynamically load the first 3 posts onto the page again. 当我单击一个按钮并再次将前3个帖子动态加载到页面上时,就会出现问题。 This loaded via .html(data). 这是通过.html(data)加载的。

I'm doing this on a "page" called archives. 我正在称为存档的“页面”上执行此操作。

It's as though the archive page knows I displayed all 11 posts before and refuses to start again unless I refresh the page. 好像存档页面知道我之前显示了所有11条帖子,除非我刷新页面,否则拒绝再次开始。

How do I reset the loop, or tell wordpress that I want to start from the 3rd post again? 如何重置循环,或告诉wordpress我想从第三篇文章开始?

jQuery(document).ready(function($){
    $("a.sort-all").bind("click", function(e) {
        load_posts(this);
        e.preventDefault();                 
    });

    function load_posts() {
        var ajax_url = $('.sort-all').attr('data-all-url');
        $.ajax({
            dataType: "HTML",
            url: ajax_url,
            type:'POST',
            data:{
                action: 'data_click_all',
                offset: offset
            },
            success:function(data) {
                $('#content').html(data);
            }
        });

    }

    var ajax_url = $('.sort-all').attr('data-all-url');
    var offset = 0;
    $('#content').waypoint(function(direction) {
        if(direction === 'down'){
            offset = parseInt(offset) + 3;
            $.ajax({
                dataType: "HTML",
                url: ajax_url,
                type:'POST',
                data:{
                    action: 'data_scroll_all',
                    offset: offset
                },
                success:function(data) {
                    $('#content').append(data);
                    $.waypoints('refresh');
                }
            });
        }
    }, {
        offset: 'bottom-in-view'
    });
}); 

if (!class_exists('load_posts')) {
    class load_posts    {
        /**
        * PHP 4 Compatible Constructor
        */
        function load_posts(){$this->__construct();}
        /**
        * PHP 5 Constructor
            */      
        function __construct(){
            add_action('wp_ajax_data_scroll_all', array(&$this, 'data_scroll_all'));
            add_action('wp_ajax_nopriv_data_scroll_all', array(&$this, 'data_scroll_all'));
            //----//
            add_action('wp_ajax_data_click_all', array(&$this, 'data_click_all'));
            add_action('wp_ajax_nopriv_data_click_all', array(&$this, 'data_click_all'));
        }

        function data_click_all(){
            global $post;
            $offset = $_POST['offset'];
            $args = array('posts_per_page' => 3, 'offset' => $offset, 'category__not_in' => 1,'orderby' => 'date');
            $myposts = get_posts( $args );
            foreach( $myposts as $post ) {
                setup_postdata($post);
                get_template_part( 'content-archive' );
            } wp_reset_query();
            die('');
        }

        function data_scroll_all(){
            global $post;
            $offset = $_POST['offset'];
            $args = array('posts_per_page' => 3, 'category__not_in' => 1, 'offset' => $offset, 'orderby' => 'date');
            $myposts = get_posts( $args );
            foreach( $myposts as $post ) {
                setup_postdata($post);
                get_template_part( 'content-archive' );
            } wp_reset_query();
            die('');
        }


    }
}
if (class_exists('load_posts')) {
    $newload_posts = new load_posts();
}

When you click a button and dynamically load the first 3 posts onto the page again, You have to reset the variable offset. 当您单击一个按钮并将前3个帖子重新动态加载到页面上时,您必须重置变量offset。 Also refresh the waypoints. 还刷新航路点。

 $("a.sort-all").bind("click", function(e) {
         offset=0;
         load_posts(this);
        $.waypoints('refresh');
        e.preventDefault();                 
    });

if it doesnt work, proably you dont have an error with the number of posts, but another error that prevents everything to work. 如果它不起作用,很可能您发布的帖子数量没有错误,但是另一个错误阻止了所有工作。

please, add an alert when you enter the waypoint 请在输入航路点时添加提醒

$('#content').waypoint(function(direction) {
      alert("current: "+offset);
///...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM