get_next_post()
云策文档标注
概述
get_next_post() 函数用于检索与当前文章相邻的下一个文章对象,基于文章发布顺序。它是 get_adjacent_post() 的封装,常用于导航链接。
关键要点
- 函数返回 WP_Post 对象、null 或空字符串,具体取决于全局 $post 设置和相邻文章是否存在。
- 支持参数控制是否在同一分类术语中检索,并可排除特定术语 ID。
- 默认使用 'category' 作为分类法,但可通过 $taxonomy 参数自定义。
代码示例
$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;注意事项
- 确保全局 $post 已设置,否则函数可能返回 null。
- 在单页中,如需在同一标签下检索,可将 $taxonomy 设置为 'post_tag'。
原文内容
Retrieves the next post that is adjacent to the current post.
Parameters
$in_same_termbooloptional-
Whether post should be in the same taxonomy term.
Default:
false $excluded_termsint[]|stringoptional-
Array or comma-separated list of excluded term IDs.
Default empty. $taxonomystringoptional-
Taxonomy, if
$in_same_termis true. Default'category'.
Source
function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 3 content
Codex
Example
$next_post = get_next_post(); if ( is_a( $next_post , 'WP_Post' ) ) : ?> <a href="<?php echo get_permalink( $next_post->ID ); ?>">ID ); ?></a>Skip to note 4 content
valery7500
In a single page, you can make a link to another post in same Tag …
The value for $taxonomy is post_tag
Example
<a href="<?php echo get_permalink( $next_post->ID ); ?>">Back</a>