我们知道 WordPress 官方提供了 previous_post_link()与 next_post_link() 这两个函数标签来制定当前文章所属分类里面的上下篇文章,但是如果要获取上下篇文章的其他参数,比如链接、标题、特色图像,实现下图的效果:我们该如何处理呢?
涉及到 WordPress 函数标签
- get_next_post
- get_previous_post
- get_permalink
- get_the_title
- get_the_post_thumbnail
实现代码
<div class="post-PrevNext">
<?php
$current_category = get_the_category();//获取当前文章所属分类ID
$prev_post = get_previous_post($current_category,'');//与当前文章同分类的上一篇文章
$next_post = get_next_post($current_category,'');//与当前文章同分类的下一篇文章
?>
<div class="previous_post_link">
<?php if (!empty( $prev_post )): ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo get_the_post_thumbnail( $prev_post->ID, '', '' ); ?></a> 上一篇: <a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo $prev_post->post_title; ?></a>
<?php endif; ?>
</div>
<div class="next_post_link">
<?php if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_post_thumbnail( $next_post->ID, '', '' ); ?></a> 下一篇: <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo $next_post->post_title; ?></a>
<?php endif; ?>
</div>
</div>