我正在使用下面的代码显示缩略图,其中包含上一篇/下一篇文章的链接single.php
页如果下一篇文章和上一篇文章来自同一个类别,但如果下一篇文章或上一篇文章链接来自另一个类别,那么它可以正常工作。
<!-- Pagination -->
<div id="post_pagination">
<?php $nextPost = get_next_post(true);
if($nextPost) {
$args = array(
\'posts_per_page\' => 1,
\'in_same_cat\' => false,
\'include\' => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>"><div class="post_next_post">
<?php the_post_thumbnail(\'thumbnail\'); ?>
<span class="next">Next Story »</span>
<h4><?php the_title(); ?></h4>
</div></a>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
<?php $prevPost = get_previous_post(true);
if($prevPost) {
$args = array(
\'posts_per_page\' => 1,
\'in_same_cat \' => false,
\'include\' => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>"><div class="post_prev_post">
<?php the_post_thumbnail(\'thumbnail\'); ?>
<span class="prev">« Previous Story</span>
<h4><?php the_title(); ?></h4>
</div></a>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</div>
<!-- Pagination -->
那么,如何使其正常工作,下一个/上一个帖子链接工作。。。???
最合适的回答,由SO网友:Muhammad Hassan 整理而成
在尝试向他人寻求帮助后,我得到了我的解决方案,我在下面分享。首先,没有使用\'in_same_cat\' => false,
在上代码和第二代码中get_next_post(true);
, get_previous_post(true);
用于显示当前帖子类别中的下一篇/上一篇帖子,但如果要在下一篇/上一篇中显示所有帖子,则只能使用get_next_post();
, get_previous_post();
而不是上面的。最后,下面共享了工作代码。
<!-- Pagination -->
<div id="post_pagination">
<?php $nextPost = get_next_post();
if($nextPost) {
$args = array(
\'posts_per_page\' => 1,
\'include\' => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>"><div class="post_next_post">
<?php the_post_thumbnail(\'thumbnail\'); ?>
<span class="next">Next Story »</span>
<h4><?php the_title(); ?></h4>
</div></a>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
<?php $prevPost = get_previous_post();
if($prevPost) {
$args = array(
\'posts_per_page\' => 1,
\'include\' => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>"><div class="post_prev_post">
<?php the_post_thumbnail(\'thumbnail\'); ?>
<span class="prev">« Previous Story</span>
<h4><?php the_title(); ?></h4>
</div></a>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</div>
<!-- Pagination -->