函数文档

previous_posts_link()

💡 云策文档标注

概述

previous_posts_link() 是 WordPress 中用于显示上一页文章链接的模板标签函数,通常用于分页导航。它基于当前查询输出指向较新文章的链接,适用于博客文章列表等场景。

关键要点

  • 函数功能:输出上一页文章链接,默认无参数时显示默认文本。
  • 参数:$label(可选字符串),用于自定义链接文本,默认值为 null。
  • 与 get_previous_posts_link() 的关系:如需在 PHP 中获取链接值而非直接输出,应使用 get_previous_posts_link()。
  • 排序逻辑:由于文章查询通常按时间倒序排列,previous_posts_link() 通常指向较新的文章(集合开头),而 next_posts_link() 指向较旧的文章(集合末尾)。
  • 注意事项:函数内部有条件判断 is_single() 为 false 时才运行,因此在自定义查询中使用时需注意放置位置,以避免在单篇文章或自定义文章类型页面上不显示分页。

代码示例

previous_posts_link( __( '« Newer Entries', 'textdomain' ) );
if( get_previous_posts_link() ) {
    previous_posts_link( '« Newer Entries' );
}

📄 原文内容

Displays the previous posts page link.

Parameters

$labelstringoptional
Previous page link text.

Default:null

More Information

If you need the values for use in PHP, use get_previous_posts_link().

Because post queries are usually sorted in reverse chronological order, next_posts_link() usually points to older entries (toward the end of the set) and previous_posts_link() usually points to newer entries (toward the beginning of the set).

Source

function previous_posts_link( $label = null ) {
	echo get_previous_posts_link( $label );
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes

  1. Skip to note 6 content

    A warning when using with custom queries

    This function previous_posts_link() has a condition to run if is_single() is false.

    This is good to know when creating custom queries and adding pagination using this function because where you place your custom query can change whether the pagination shows or not.

    What’s interesting is that:
    is_single() doesn’t work on pages (or media)
    is_single() does work on CPTs and posts

    So custom queries with this pagination placed on a single page will work fine, but add the same custom query + pagination to a single CPT or post, and it will not show.