使用多个查询加载更多帖子

时间:2013-02-20 作者:user156

我一直在开发一个使用load more ajax插件的主题,所有内容都已设置完毕,可以很好地处理主查询。

但是,一旦我为热门帖子设置了新的查询,热门帖子的加载更多帖子导航将加载主查询的下一篇帖子。有没有办法让它加载热门帖子的下一个帖子页面?

以下是索引文件:

<?php get_header(); ?>

    <div id="blog">
      <h2 class="recent-head">Recent Posts</h2>
        <div class="recent-home-posts">
          <?php $paged = (get_query_var(\'paged\')) ? (int) get_query_var(\'paged\') : 1;
          $my_query = new WP_Query( array( \'posts_per_page\' => 2, \'paged\' => $paged ) );
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
            <div class="post clearfix">
              <div class="thumb">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'home-thumb\'); ?></a>
                  <span class="cat"><?php the_category(\' \') ?></span>
                  <span class="comme"><?php comments_popup_link(\'0\', \'1\', \'%\'); ?></span>
              </div>
              <div class="entry">   
                  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                  <span class="time"><?php the_time(\'d,F,Y\'); ?></span>
                   <p><?php echo excerpt(40); ?></p>
                  <a class="read-more" href="<?php the_permalink(); ?>">Read More</a>
              </div>
            </div>
            <?php endwhile;?>
            <?php include( TEMPLATEPATH . \'/load-more.php\' );?>
        </div>

      <h2 class="recent-head">Popular Posts</h2>
        <div class="top-home-posts clearfix">
        <div class="topwrap">
<?php 
$paged = (get_query_var(\'paged\')) ? (int) get_query_var(\'paged\') : 1;
$popularpost = new WP_Query( array( \'posts_per_page\' => 3, \'orderby\' => \'comment_count\', \'paged\' => $paged  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();  ?>
                <div class="top-post">
                    <div class="top-thumb">
                      <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'popular-thumb\'); ?></a>
                        <span class="cat"><?php the_category(\' \') ?></span>
                        <span class="comme"><?php comments_popup_link(\'0\', \'1\', \'%\'); ?></span>
                    </div>
                    <div class="top-entry"> 
                      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <span class="time"><?php the_time(\'d,F,Y\'); ?></span>
                        <p><?php echo excerpt(20); ?></p>
                        <a class="read-more" href="<?php the_permalink(); ?>">Read More</a>
                    </div>
                </div>
                  <?php endwhile; ?>
            <div class="break"></div>
<div class="navigation-top">
    <?php next_posts_link(\'Older &raquo;\') ?>
</div>
        </div>
        </div>
    </div>
<?php get_sidebar(); ?> 
<?php get_footer(); ?>
这是load more posts插件代码:

jQuery(document).ready(function($) {
        <?php 
$paged = (get_query_var(\'paged\')) ? (int) get_query_var(\'paged\') : 1;
$popularpost = new WP_Query( array( \'posts_per_page\' => 3, \'orderby\' => \'comment_count\', \'paged\' => $paged  ) );  ?>
var pbd_alp = {
        startPage: "1",
        maxPages: "<?php echo $popularpost->max_num_pages; ?>",
        nextLink: "<?php bloginfo(\'url\'); ?>/page/2/"
};
    var pageNum = parseInt(pbd_alp.startPage) + 1;

    // The maximum number of pages the current query can return.
    var max = parseInt(pbd_alp.maxPages);

    // The link of the next page of posts.
    var nextLink = pbd_alp.nextLink;

    /**
     * Replace the traditional navigation with our own,
     * but only if there is at least one page of new posts to load.
     */
    if(pageNum <= max) {
        // Insert the "More Posts" link.
        $(\'.top-home-posts\')
            .append(\'<div class="more-top-posts-page-\'+ pageNum +\'"></div>\')
            .append(\'<div id="load-more-top"><a href="#">Load More Posts</a></div>\');

        // Remove the traditional navigation.
        $(\'.navigation-top a\').remove();
    }


    /**
     * Load new posts when the link is clicked.
     */
    $(\'#load-more-top a\').click(function() {

        // Are there more posts to load?
        if(pageNum <= max) {

            // Show that we\'re working.
            $(this).text(\'Loading posts...\');

            $(\'.more-top-posts-page-\'+ pageNum).load(nextLink + \' .post\',
                function() {
                    // Update page number and nextLink.
                    pageNum++;
                    nextLink = nextLink.replace(/\\/page\\/[0-9]?/, \'/page/\'+ pageNum);

                    // Add a new placeholder, for when user clicks again.
                    $(\'#load-more-top\')
                        .before(\'<div class="more-top-posts-page-\'+ pageNum +\'"></div>\')

                    // Update the button message.
                    if(pageNum <= max) {
                        $(\'#load-more-top a\').text(\'Load More Posts\');
                    } else {
                        $(\'#load-more-top a\').text(\'No more posts to load.\');
                    }
                }
            );
        } else {
            $(\'#load-more-top a\').append(\'.\');
        }   

        return false;
    });
});
感谢您的帮助/建议。

1 个回复
SO网友:AJ Zane

看起来您没有重置查询,因此WordPress正在对您已经收到的帖子运行新的查询。

在第一次查询后添加:

wp_reset_query();
Always 在任何不是循环的查询之后重置。

这是上面的法典页:http://codex.wordpress.org/Function_Reference/wp_reset_query

结束

相关推荐

AJAX对不起作用的潜在用户

我有一个非常简单的AJAX函数,当以admin身份登录时可以正常工作,但当以订阅者身份登录时,可以调用admin AJAX。php返回一个html页面,其中包含我的主题的页眉和页脚,仅此而已。通过做一些狼击剑,我追踪到了管理ajax的第37行。phpdo_action( \'admin_init\' );. 它到了那里就回来了。