WordPress函数:get_previous_post 获取前一篇文章

江河/ 2023年05月21日/ WordPress/ 浏览 1028

函数原型:


get_previous_post( bool $in_same_term = false, int[]|string $excluded_terms = '', string $taxonomy = 'category' ): WP_Post|null|string


获取当前文章的前一篇文章。


函数源码:


function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
	return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
}


get_previous_post 在内部直接调用了函数 get_adjacent_post,参数几乎都是一模一样的,只是设置了默认值,让我们使用更方便。可参考文章:《WordPress函数:get_adjacent_post 获取相邻的(上一篇下一篇)文章》


使用举例:


<?php
$prev_post = get_previous_post();
if ( ! empty( $prev_post ) ): ?>
	<a href="<?php echo get_permalink( $prev_post->ID ); ?>">
		<?php echo apply_filters( 'the_title', $prev_post->post_title ); ?>
	</a>
<?php endif; ?>


此需求也可使用函数 get_previous_post_link 实现。


发表评论

暂无评论,抢个沙发...

客服 工单