函数文档

get_query_pagination_arrow()

💡 云策文档标注

概述

get_query_pagination_arrow() 是一个辅助函数,用于根据 QueryPagination 上下文中的 paginationArrow 参数,为 QueryPaginationNext 和 QueryPaginationPrevious 块生成正确的分页箭头 HTML。

关键要点

  • 函数用于 QueryPaginationNext 和 QueryPaginationPrevious 块,基于 paginationArrow 上下文返回箭头 HTML。
  • 接受两个参数:$block(WP_Block 实例)和 $is_next(布尔值,指示是否为下一个块)。
  • 返回字符串(箭头 HTML)或 null(如果没有箭头)。
  • 支持三种箭头类型:'none'(无箭头)、'arrow'(→ 和 ←)、'chevron'(» 和 «)。
  • 函数内部使用映射数组 $arrow_map 来定义箭头符号。

代码示例

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

注意事项

  • 函数从 WordPress 5.9.0 版本开始引入。
  • 需要确保 $block 参数包含有效的 paginationArrow 上下文,否则可能返回 null。

📄 原文内容

Helper function that returns the proper pagination arrow HTML for QueryPaginationNext and QueryPaginationPrevious blocks based on the provided paginationArrow from QueryPagination context.

Description

It’s used in QueryPaginationNext and QueryPaginationPrevious blocks.

Parameters

$blockWP_Blockrequired
Block instance.
$is_nextboolrequired
Flag for handling next/previous blocks.

Return

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

Source

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

Changelog

Version Description
5.9.0 Introduced.