【问题标题】:displaying wordpress recent posts on a template page with previous and next controls在带有上一个和下一个控件的模板页面上显示 wordpress 最近的帖子
【发布时间】:2014-02-24 11:02:20
【问题描述】:

我正在使用 wp-query 在模板页面上显示 5 个最新帖子。 它工作正常,我还想添加上一个和下一个按钮来导航到前 5 个帖子,等等。

我设置了'showposts=5',它会显示最近的 5 个帖子,这很完美,但是当点击“上一个”时,它会将我重定向到 /page/2/,但会显示 5 个相同的帖子...

这是我的代码:

<?php
    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

      <h2><a href="<?php the_permalink() ?>"><?php the_time('y-m-d'); ?> | <?php the_title(); ?></a></h2>
<?php endwhile; ?>

    <?php if ($paged > 1) { ?>

    <nav id="nav-posts">
      <div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
      <div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
    </nav>

    <?php } else { ?>

    <nav id="nav-posts">
      <div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
    </nav>

    <?php } ?>

    <?php wp_reset_postdata(); ?>

任何人都可以帮助我吗? 不知道哪里出了问题……

感谢您的帮助

【问题讨论】:

    标签: php wordpress posts


    【解决方案1】:

    我认为你没有使用 $paged 变量;

    使用$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    在 WP_Query() 函数之前

    【讨论】:

    • 感谢您的回复@anstrangel0ver,但我不明白目的..你能解释一下吗?
    • 其实上面的代码就是get_query_var('paged')从urlurl/page/2获取值2。如果没有设置页面,它将给出值 1。分页参数用于定义它在哪个页面上。因此,如果 $paged 为 1,它将显示前 5 个帖子,如果 $page 为 2,它将显示从 6 到 10 的帖子.. 继续
    • 感谢@anstrangel0ver,我现在正在使用它,但它仍然不起作用... query('showposts=5' . '&paged='.$paged);而 ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    • 你知道为什么吗?我的 wp-includes 代码是否有问题?我没有改变里面的任何东西
    • 检查 :- 是否仍然显示相同的 5 个帖子?你有超过 5 个帖子吗?如果是,则尝试输入 $paged=2 并检查
    【解决方案2】:

    你好用分页显示最近的帖子

    <?php if ( have_posts() ) : ?>
    
    <!-- Add the pagination functions here. -->
    
    <!-- Start of the main loop. -->
    <?php while ( have_posts() ) : the_post();  ?>
    
    <!-- the rest of your theme's main loop -->
    
    <?php endwhile; ?>
    <!-- End of the main loop -->
    
    <!-- Add the pagination functions here. -->
    
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多