简体   繁体   English

使用上一个和下一个控件在模板页面上显示wordpress最近的帖子

[英]displaying wordpress recent posts on a template page with previous and next controls

I'm using a wp-query to display on a template page the 5 most recent posts. 我正在使用wp查询在模板页面上显示5个最新帖子。 it works fine, I want also to add previous and next button to navigate to the previous 5 posts, and so on. 它工作正常,我还想添加上一个和下一个按钮以导航到前5个帖子,依此类推。

I set 'showposts=5', it displays the 5 most recents post, that's perfect, but when clicking on "previous", it redirect me to /page/2/, but the 5 same posts are displayed... 我设置了“ showposts = 5”,它显示了5个最新帖子,这很完美,但是当单击“上一个”时,它将我重定向到/ page / 2 /,但是显示了5个相同的帖子...

here is my code : 这是我的代码:

<?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(); ?>

anyone can help me with this ? 有人可以帮助我吗? I don't know where's the problem... 我不知道问题出在哪里...

thanks for your help 谢谢你的帮助

i think you are not using the $paged variable; 我认为您没有使用$ paged变量;

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

before the WP_Query() function 在WP_Query()函数之前

Hello Display Recent Post with Pagination 你好显示最近的分页帖子

<?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; ?>

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

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