函数文档

get_the_comments_pagination()

💡 云策文档标注

概述

get_the_comments_pagination() 函数用于检索评论分页导航的 HTML 标记,适用于需要显示评论分页的场景。它基于 paginate_comments_links() 构建,并支持自定义参数以增强可访问性和样式。

关键要点

  • 函数返回评论分页导航的字符串标记,不直接输出。
  • 参数 $args 为可选数组,可设置 screen_reader_text、aria_label 和 class 等属性。
  • 内部使用 paginate_comments_links() 生成链接,并通过 _navigation_markup() 包装导航标记。
  • 确保 aria_label 属性存在,若未提供则回退到 screen_reader_text。
  • 如果 $args['type'] 设置为 'array',会自动转换为 'plain' 以确保返回字符串。

代码示例

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

    // 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(
            'screen_reader_text' => __( 'Comments pagination' ),
            'aria_label'         => __( 'Comments pagination' ),
            'class'              => 'comments-pagination',
        )
    );
    $args['echo'] = false;

    // Make sure we get a string back. Plain is the next best thing.
    if ( isset( $args['type'] ) && 'array' === $args['type'] ) {
        $args['type'] = 'plain';
    }

    $links = paginate_comments_links( $args );

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

    return $navigation;
}

注意事项

  • 此函数从 WordPress 4.4.0 版本引入,后续版本添加了 aria_label 和 class 参数。
  • 与 the_comments_pagination() 不同,它仅返回标记而不直接输出,适合在模板中自定义使用。
  • 确保参数设置符合可访问性标准,如提供 screen_reader_text 和 aria_label。

📄 原文内容

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

Description

See also

Parameters

$argsarrayoptional
Default pagination arguments.

  • screen_reader_text string
    Screen reader text for the nav element. Default ‘Comments pagination’.
  • aria_label string
    ARIA label text for the nav element. Default ‘Comments pagination’.
  • class string
    Custom class for the nav element. Default 'comments-pagination'.

Default:array()

Return

string Markup for pagination links.

Source

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

	// 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(
			'screen_reader_text' => __( 'Comments pagination' ),
			'aria_label'         => __( 'Comments pagination' ),
			'class'              => 'comments-pagination',
		)
	);
	$args['echo'] = false;

	// Make sure we get a string back. Plain is the next best thing.
	if ( isset( $args['type'] ) && 'array' === $args['type'] ) {
		$args['type'] = 'plain';
	}

	$links = paginate_comments_links( $args );

	if ( $links ) {
		$navigation = _navigation_markup( $links, $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.