函数文档

posts_nav_link()

💡 云策文档标注

概述

posts_nav_link() 函数用于显示文章列表页的上一页和下一页导航链接。它接受可选参数来自定义分隔符和标签,并依赖于 get_posts_nav_link() 函数来生成链接。

关键要点

  • 函数功能:输出文章列表页的导航链接,包括“上一页”和“下一页”。
  • 参数说明:$sep(分隔符,默认空)、$prelabel(上一页标签,默认空)、$nxtlabel(下一页标签,默认空)。
  • 相关函数:next_posts_link() 和 previous_posts_link() 用于类似目的,但需单独调用;next_post_link() 和 previous_post_link() 用于单个文章导航。
  • 注意事项:WordPress 中“下一页”定义为“向过去方向的下一页”,因为文章通常按时间倒序排列。
  • 默认输出:默认样式为“« Previous Page — Next Page »”。
  • 代码示例:函数内部调用 get_posts_nav_link(),开发者应避免错误使用如 Kubrick 主题中的不当代码。

代码示例

function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
    $args = array_filter( compact( 'sep', 'prelabel', 'nxtlabel' ) );
    echo get_posts_nav_link( $args );
}

注意事项

  • 避免在主题中错误使用 posts_nav_link(),例如 Kubrick 主题的旧代码可能导致重复链接或错误导航。
  • 推荐使用 next_posts_link() 和 previous_posts_link() 来分别控制上一页和下一页链接,以提高灵活性和正确性。

📄 原文内容

Displays the post pages link navigation for previous and next pages.

Parameters

$sepstringoptional
Separator for posts navigation links. Default empty.
$prelabelstringoptional
Label for previous pages. Default empty.
$nxtlabelstringoptional
Optional Label for next pages. Default empty.

More Information

For displaying next and previous pages of posts see next_posts_link() and previous_posts_link().

For displaying next and previous post navigation on individual posts, see next_post_link() and previous_post_link().

Note: since weblog posts are traditionally listed in reverse chronological order (with most recent posts at the top), there is some ambiguity in the definition of “next page”. WordPress defines “next page” as the “next page toward the past“.

Source

function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
	$args = array_filter( compact( 'sep', 'prelabel', 'nxtlabel' ) );
	echo get_posts_nav_link( $args );
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes

  1. Skip to note 9 content

    Kubrick Theme Format

    The Kubrick theme format for posts navigation, could be formatted this way. However, using posts_nav_link() in this way will result in unintended behavior, such as double stacked next and previous links that link to the incorrect sections.

    The Kubrick Theme actually uses next_posts_link() and previous_posts_link() .

    This is poor code and should not be used:

    <div class="navigation">
    <div class="alignleft"></div>
    <div class="alignright"></div>
    </div>

    This is better code:

    <div class="navigation">
    <div class="alignleft"></div>
    <div class="alignright"></div>
    </div>