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

追格官方小助手/ 2023年02月13日/ WordPress/ 浏览 1018

WordPress 中如何【下一篇】文章的链接?


可以参考之前的文章《WordPress函数:get_adjacent_post 获取相邻的文章》,也可以使用参数:get_next_post 。


函数原型:


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


参数说明:


$in_same_term,是否要求与当前文章是同一类别。默认无要求。


$excluded_terms,要排除哪些类型。默认不排除。


$taxonomy, 分类方式,默认为 post 的分类 category。


函数源码:


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


使用举例:


<?php $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; ?>


发表评论

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

客服 工单