[英]displaying wordpress recent posts on a template page with previous and next controls
我正在使用wp查詢在模板頁面上顯示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(); ?>
有人可以幫助我嗎? 我不知道問題出在哪里...
謝謝你的幫助
我認為您沒有使用$ paged變量;
使用$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
在WP_Query()函數之前
你好顯示最近的分頁帖子
<?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.