WordPress函数:get_next_post 获取下一篇文章

江河/ 2023年05月22日/ WordPress/ 浏览 715

函数原型:


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


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


函数源码:


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


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


使用举例:


$next_post = get_next_post();
if ( is_a( $next_post , 'WP_Post' ) ) : ?>
	<a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_title( $next_post->ID ); ?></a>
<?php endif; ?>


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


发表评论

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

客服 工单