加载更多帖子插件


Load more posts plugin

我编辑了这个php文件,一次加载12篇文章,但我意识到代码也将加载的页面数量最大化为12。在第12页之后,它说"不再加载帖子",尽管仍然有帖子。我应该如何编辑这段代码,以便每次加载12个帖子,加载无限的页面?

<?php
function pbd_alp_init() {
global $wp_query;
// Add code to index pages.
//if( !is_singular() ) {    
    if( !is_single() ) {
    // Queue JS and CSS
    wp_enqueue_script(
        'pbd-alp-load-posts',
        plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
        array('jquery'),
        '1.0',
        true
    );
    wp_enqueue_style(
        'pbd-alp-style',
        plugin_dir_url( __FILE__ ) . 'css/style.css',
        false,
        '1.0',
        'all'
    );

    // What page are we on? And what is the pages limit?
    $max = $wp_query->max_num_pages;
            if(is_page_template('template-blog.php')){
        $max = wp_count_posts()->publish;
        //default is 6 posts per page
        if($max % 12 == 0){
            $max = $max / 12;
        }else{
            $max = (int)($max / 12) + 1;
        }
    }
    $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
    // Add some parameters for the JS.
    wp_localize_script(
        'pbd-alp-load-posts',
        'pbd_alp',
        array(
            'startPage' => $paged,
            'maxPages' => $max,
            'nextLink' => next_posts($max, false)
        )
    );
}

}add_action('template_redirect','pbd_alp_init');

?>

您不希望一次加载无限个页面。你想发送一个12-36的查询,这取决于你的网站流量。然后每个后续的请求都会得到下一个。使用count()计算帖子总数,并执行for循环。将最后一个post ID保存在javascript中,以便下一个ajax调用在+12开始,等等。这好吗?或者你需要我帮你手工编写代码吗?