【问题标题】:In WordPress, when using query_posts(), why does pagination not work?在 WordPress 中,使用 query_posts() 时,为什么分页不起作用?
【发布时间】:2016-07-11 12:23:21
【问题描述】:

这是 index.php 中的代码。当我点击“旧帖子”时,它仍然显示第一页内容。只有默认循环适用于分页。

    <?php 
    query_posts('showposts=10');
    query_posts("cat=2");
    if( have_posts() ):

        while( have_posts() ): the_post(); ?>

            <?php get_template_part('content',get_post_format()); ?>

        <?php endwhile; ?>

            <?php next_posts_link('« Older Posts'); ?>
            <?php previous_posts_link('Newer Posts »'); ?>

    <?php endif;
        wp_reset_query();   
    ?>  

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    query_posts() 不建议以这种方式使用,因为它会覆盖主查询,并且特定于您的情况,默认情况下为does not support pagination。您可能应该使用get_posts() 或使用WP_Query 对象。

    如果您必须使用query_posts(),那么上面的链接中有关于如何将paged 参数添加到查询的说明。

    这是相同的代码,但使用了get_posts()

    <?php 
    $args = array('numberposts' => 10, 'category' => 2);
    $posts = get_posts($args);
    foreach($posts as $post) {   
        // Use the $post object here in your content template
        <?php get_template_part('content',get_post_format()); ?>
    }
    <?php next_posts_link('« Older Posts'); ?>
    <?php previous_posts_link('Newer Posts »'); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多