函数文档

get_comments_pagination_arrow()

💡 云策文档标注

概述

get_comments_pagination_arrow() 是一个辅助函数,用于根据 CommentsPagination 上下文中的 paginationArrow 设置,为 CommentsPaginationNext 和 CommentsPaginationPrevious 块生成正确的分页箭头 HTML。

关键要点

  • 函数返回分页箭头的 HTML 字符串或 null(如果无箭头)。
  • 参数包括必需的 $block(WP_Block 实例)和可选的 $pagination_type(默认为 'next',接受 'next' 或 'previous')。
  • 箭头类型基于 $block->context['comments/paginationArrow'] 映射,支持 'none'、'arrow' 和 'chevron'。

代码示例

function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
    $arrow_map = array(
        'none'    => '',
        'arrow'   => array(
            'next'     => '→',
            'previous' => '←',
        ),
        'chevron' => array(
            'next'     => '»',
            'previous' => '«',
        ),
    );
    if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
        $arrow_attribute = $block->context['comments/paginationArrow'];
        $arrow           = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
        $arrow_classes   = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
        return "$arrow";
    }
    return null;
}

注意事项

  • 此函数在 WordPress 6.0.0 版本中引入。
  • 主要用于 CommentsPaginationNext 和 CommentsPaginationPrevious 块内部。

📄 原文内容

Helper function that returns the proper pagination arrow HTML for CommentsPaginationNext and CommentsPaginationPrevious blocks based on the provided paginationArrow from CommentsPagination context.

Description

It’s used in CommentsPaginationNext and CommentsPaginationPrevious blocks.

Parameters

$blockWP_Blockrequired
Block instance.
$pagination_typestringoptional
Type of the arrow we will be rendering.
Accepts 'next' or 'previous'. Default 'next'.

Return

string|null The pagination arrow HTML or null if there is none.

Source

function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) {
	$arrow_map = array(
		'none'    => '',
		'arrow'   => array(
			'next'     => '→',
			'previous' => '←',
		),
		'chevron' => array(
			'next'     => '»',
			'previous' => '«',
		),
	);
	if ( ! empty( $block->context['comments/paginationArrow'] ) && ! empty( $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ] ) ) {
		$arrow_attribute = $block->context['comments/paginationArrow'];
		$arrow           = $arrow_map[ $block->context['comments/paginationArrow'] ][ $pagination_type ];
		$arrow_classes   = "wp-block-comments-pagination-$pagination_type-arrow is-arrow-$arrow_attribute";
		return "<span class='$arrow_classes' aria-hidden='true'>$arrow</span>";
	}
	return null;
}

Changelog

Version Description
6.0.0 Introduced.