函数文档

get_the_comments_navigation()

💡 云策文档标注

概述

get_the_comments_navigation() 函数用于在评论分页时生成导航链接,返回包含上一页和下一页评论链接的 HTML 标记。它仅在评论页面数大于1时输出导航。

关键要点

  • 函数返回字符串类型的评论导航标记,适用于多页评论场景。
  • 接受一个可选数组参数 $args,用于自定义导航文本、ARIA 标签、CSS 类等属性。
  • 内部依赖 get_previous_comments_link() 和 get_next_comments_link() 生成链接,并使用 _navigation_markup() 包装导航结构。
  • 默认参数包括 prev_text、next_text、screen_reader_text、aria_label 和 class,可通过 $args 覆盖。
  • 在 WordPress 5.5.0 版本添加了 class 参数,5.3.0 添加了 aria_label 参数,最初于 4.4.0 版本引入。

代码示例

function get_the_comments_navigation( $args = array() ) {
    $navigation = '';

    // Are there comments to navigate through?
    if ( get_comment_pages_count() > 1 ) {
        // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
        if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
            $args['aria_label'] = $args['screen_reader_text'];
        }

        $args = wp_parse_args(
            $args,
            array(
                'prev_text'          => __( 'Older comments' ),
                'next_text'          => __( 'Newer comments' ),
                'screen_reader_text' => __( 'Comments navigation' ),
                'aria_label'         => __( 'Comments' ),
                'class'              => 'comment-navigation',
            )
        );

        $prev_link = get_previous_comments_link( $args['prev_text'] );
        $next_link = get_next_comments_link( $args['next_text'] );

        if ( $prev_link ) {
            $navigation .= '' . $prev_link . '';
        }

        if ( $next_link ) {
            $navigation .= '' . $next_link . '';
        }

        $navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
    }

    return $navigation;
}

注意事项

  • 函数仅在 get_comment_pages_count() 返回大于1时生成导航,否则返回空字符串。
  • 如果提供了 screen_reader_text 但未设置 aria_label,函数会自动将 aria_label 设置为 screen_reader_text 的值。
  • 相关函数包括 the_comments_navigation()(用于直接显示导航)、get_previous_comments_link()、get_next_comments_link() 和 _navigation_markup()。

📄 原文内容

Retrieves navigation to next/previous set of comments, when applicable.

Parameters

$argsarrayoptional
Default comments navigation arguments.

  • prev_text string
    Anchor text to display in the previous comments link.
    Default ‘Older comments’.
  • next_text string
    Anchor text to display in the next comments link.
    Default ‘Newer comments’.
  • screen_reader_text string
    Screen reader text for the nav element. Default ‘Comments navigation’.
  • aria_label string
    ARIA label text for the nav element. Default 'Comments'.
  • class string
    Custom class for the nav element. Default 'comment-navigation'.

Default:array()

Return

string Markup for comments links.

Source

function get_the_comments_navigation( $args = array() ) {
	$navigation = '';

	// Are there comments to navigate through?
	if ( get_comment_pages_count() > 1 ) {
		// Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
		if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
			$args['aria_label'] = $args['screen_reader_text'];
		}

		$args = wp_parse_args(
			$args,
			array(
				'prev_text'          => __( 'Older comments' ),
				'next_text'          => __( 'Newer comments' ),
				'screen_reader_text' => __( 'Comments navigation' ),
				'aria_label'         => __( 'Comments' ),
				'class'              => 'comment-navigation',
			)
		);

		$prev_link = get_previous_comments_link( $args['prev_text'] );
		$next_link = get_next_comments_link( $args['next_text'] );

		if ( $prev_link ) {
			$navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
		}

		if ( $next_link ) {
			$navigation .= '<div class="nav-next">' . $next_link . '</div>';
		}

		$navigation = _navigation_markup( $navigation, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
	}

	return $navigation;
}

Changelog

Version Description
5.5.0 Added the class parameter.
5.3.0 Added the aria_label parameter.
4.4.0 Introduced.