使用上一个和下一个控件在模板页面上显示wordpress最近的帖子
问题描述:
I'm using a wp-query to display on a template page the 5 most recent posts. it works fine, I want also to add previous and next button to navigate to the previous 5 posts, and so on.
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...
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;
use $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
before the WP_Query() function
答
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; ?>