get_default_block_categories()
云策文档标注
概述
get_default_block_categories() 函数返回块类型的默认类别列表,用于在块编辑器(如 Gutenberg)中组织内容块。该函数返回一个包含多个关联数组的数组,每个数组定义了一个类别的 slug、标题和图标。
关键要点
- 函数返回块类型的默认类别数组,每个类别包含 'slug'、'title' 和 'icon' 键。
- 类别包括 text、media、design、widgets、theme、embed 和 reusable(在 6.3.0 版本中重命名为 Patterns)。
- 使用 _x() 函数进行本地化,支持多语言翻译。
- 函数无参数,直接返回静态定义的数组。
代码示例
function get_default_block_categories() {
return array(
array(
'slug' => 'text',
'title' => _x( 'Text', 'block category' ),
'icon' => null,
),
array(
'slug' => 'media',
'title' => _x( 'Media', 'block category' ),
'icon' => null,
),
array(
'slug' => 'design',
'title' => _x( 'Design', 'block category' ),
'icon' => null,
),
array(
'slug' => 'widgets',
'title' => _x( 'Widgets', 'block category' ),
'icon' => null,
),
array(
'slug' => 'theme',
'title' => _x( 'Theme', 'block category' ),
'icon' => null,
),
array(
'slug' => 'embed',
'title' => _x( 'Embeds', 'block category' ),
'icon' => null,
),
array(
'slug' => 'reusable',
'title' => _x( 'Patterns', 'block category' ),
'icon' => null,
),
);
}注意事项
- 在 WordPress 6.3.0 版本中,'reusable' 类别的标题从 'Reusable Blocks' 更改为 'Patterns',但 slug 保持不变。
- 所有类别的 'icon' 键值均为 null,表示未指定特定图标。
- 此函数被 get_default_block_editor_settings() 和 get_block_categories() 等函数调用,用于块编辑器设置。
原文内容
Returns the list of default categories for block types.
Source
function get_default_block_categories() {
return array(
array(
'slug' => 'text',
'title' => _x( 'Text', 'block category' ),
'icon' => null,
),
array(
'slug' => 'media',
'title' => _x( 'Media', 'block category' ),
'icon' => null,
),
array(
'slug' => 'design',
'title' => _x( 'Design', 'block category' ),
'icon' => null,
),
array(
'slug' => 'widgets',
'title' => _x( 'Widgets', 'block category' ),
'icon' => null,
),
array(
'slug' => 'theme',
'title' => _x( 'Theme', 'block category' ),
'icon' => null,
),
array(
'slug' => 'embed',
'title' => _x( 'Embeds', 'block category' ),
'icon' => null,
),
array(
'slug' => 'reusable',
'title' => _x( 'Patterns', 'block category' ),
'icon' => null,
),
);
}
Skip to note 2 content
Huzaifa Al Mesbah
This code defines a function called
get_default_block_categories()that returns an array of default block categories for a block-based content editor, like the one used in WordPress’s Gutenberg editor. Block-based editors allow users to create content by assembling different types of “blocks,” such as text, images, videos, and more. These blocks are categorized to make it easier for users to find and use them. Let’s break down the code step by step:The function is defined using the function keyword:
function get_default_block_categories() { ... }. This function doesn’t take any arguments and will return an array of block categories.Inside the function, an array is defined that holds information about each block category. Each block category is represented as an associative array with three keys:
'slug', 'title', and 'icon'.The
'slug'key is a short identifier for the block category. It’s used to uniquely identify and reference the category.The ‘
title'key holds the display name of the block category. The_x()function is used for localization, allowing the block category name to be translated into different languages.The
'icon'key represents the icon associated with the block category. In this code snippet, the value is null, indicating that no specific icon is assigned to these categories.The array contains seven block category definitions, each with a unique
'slug', a localized'title', and a null'icon'.'text': Represents a category for text-related blocks.'media': Represents a category for media-related blocks, such as images and videos.'design': Represents a category for design-related blocks.'widgets': Represents a category for widget-related blocks.'theme': Represents a category for blocks related to the overall theme of the content.'embed': Represents a category for embed-related blocks, like embedding external content.'reusable': Represents a category for reusable pattern blocks.After defining all the block categories in the array, the function ends with a closing curly brace: }.
When this function is called, it will return an array containing these default block categories, which can then be used by a block-based content editor to organize and present different types of content blocks to the user in a structured manner.