函数文档

register_block_pattern_category()

💡 云策文档标注

概述

register_block_pattern_category() 函数用于在 WordPress 中注册一个新的区块模式分类。它接受分类名称和属性数组作为参数,并返回布尔值表示注册是否成功。

关键要点

  • 函数参数:$category_name(字符串,必需,包含命名空间的模式分类名称)和 $category_properties(数组,必需,区块模式的属性列表,具体参数参考 WP_Block_Pattern_Categories_Registry::register())。
  • 返回值:布尔值,成功注册返回 true,否则返回 false。
  • 内部实现:函数调用 WP_Block_Pattern_Categories_Registry::get_instance()->register() 来执行注册。
  • 引入版本:WordPress 5.5.0。
  • 相关函数:_register_core_block_patterns_and_categories() 用于注册核心区块模式和分类。

代码示例

register_block_pattern_category(
    'hero',
    array( 'label' => __( 'Hero', 'wpdocs-my-plugin' ) )
);

注意事项

  • 建议在 init hook 中调用 register_block_pattern_category(),以确保正确注册。
  • 核心已存在的分类包括:buttons、columns、gallery、header、text、featured 和 query。
  • 可以通过 wp.data.select('core').getBlockPatternCategories() 在 JavaScript 中获取所有已注册的分类列表。

📄 原文内容

Registers a new pattern category.

Parameters

$category_namestringrequired
Pattern category name including namespace.
$category_propertiesarrayrequired
List of properties for the block pattern.
See WP_Block_Pattern_Categories_Registry::register() for accepted arguments.

Return

bool True if the pattern category was registered with success and false otherwise.

Source

function register_block_pattern_category( $category_name, $category_properties ) {
	return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
}

Changelog

Version Description
5.5.0 Introduced.

User Contributed Notes