函数文档

get_previous_post()

💡 云策文档标注

概述

get_previous_post() 函数用于检索与当前文章相邻的前一篇文章,基于文章发布顺序。它是 get_adjacent_post() 的包装函数,支持按分类法筛选和排除特定术语。

关键要点

  • 函数返回 WP_Post 对象、null 或空字符串,具体取决于文章存在性和全局 $post 设置。
  • 参数包括 $in_same_term(布尔值,默认为 false)、$excluded_terms(数组或逗号分隔的 ID 列表)和 $taxonomy(字符串,默认为 'category')。
  • 相关函数有 get_adjacent_post() 和已弃用的 previous_post()。
  • 自 WordPress 1.5.0 版本引入,无重大变更记录。

代码示例

<?php
$prev_post = get_previous_post();
if ( $prev_post ) {
    echo '<a href="' . get_permalink( $prev_post->ID ) . '">' . $prev_post->post_title . '</a>';
}
?>

📄 原文内容

Retrieves the previous 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_term is true. Default 'category'.

Return

WP_Post|null|string Post object if successful. Null if global $post is not set.
Empty string if no corresponding post exists.

Source

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

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes