1
所以基本上我想循环出3个职位,并添加下一个/上一页按钮来回来列出3新/旧每次。然而,这并不像预期的那样工作,我拉着头发试图弄清楚什么是错的。WordPress的get_next_posts循环
发生什么是我得到一个“下一步”按钮,我可以点击它,它将我导航到/ page/2,但显示相同的3个结果和“Previous”按钮不显示。我哪里错了? :(
<?php
/*
Template Name: News Listing
*/
?>
<div id="news-list">
<h1>Wow thats great <span>news</span></h1>
<div class="handwritten">Uncensored and timely truth from the brewery</div>
<div class="news-container">
<?php
// set up or arguments for our custom query
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'category_name' => '',
'posts_per_page' => 3,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query($query_args);
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); // run the loop ?>
<div class="news-post">
<?php
$feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
?>
<img src="<?= $feat_image; ?>" />
<h2><?php echo the_title(); ?></h2>
<?php the_excerpt(); ?>
<a href="#">Read more</a>
<p class="published-date">Posted <?php the_time('d F');?></p>
</div>
<?php endwhile; ?>
<div class="clearfix"></div>
</div>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="next-prev">
<div class="link prev">
<?php echo get_previous_posts_link('<span class="handwritten">Next post</span><h4>That way</h4>'); // display older posts link ?>
</div>
<div class="link next">
<?php echo get_next_posts_link('<span class="handwritten">Next post</span><h4>This way</h4>', $the_query->max_num_pages); // display older posts link ?>
</div>
</div>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
<div class="clearfix"></div>
</div>
当然,你有检查'$ paged'实际包含的值是多少,对吗? – CBroe
@CBroe你是什么意思?当我回显$ paged时,我什么都没有收到 – Codehiker
@CBroe好的,你指出我的方向是正确的如果页面不是默认的静态主页你应该使用get_query_var('paged')来代替get_query_var('page')页面 – Codehiker