get_block_categories

函数
get_block_categories ( $post_or_block_editor_context )
参数
  • (WP_Post|WP_Block_Editor_Context) $post_or_block_editor_context The current post object or the block editor context.
    Required:
返回值
  • (array[]) Array of categories for block types.
定义位置
相关方法
get_default_block_categoriesget_categoriesregister_block_core_categorieswp_get_post_categoriesrender_block_core_categories
引入
5.0.0
弃用
-

get_block_categories: 这个函数返回一个区块编辑器的类别数组,每个类别包含一个slug、标题和可选的图标属性。它不接受任何参数。

返回所有将显示在区块编辑器中的区块类型的类别。

function get_block_categories( $post_or_block_editor_context ) {
	$block_categories     = get_default_block_categories();
	$block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
		new WP_Block_Editor_Context(
			array(
				'post' => $post_or_block_editor_context,
			)
		) : $post_or_block_editor_context;

	/**
	 * Filters the default array of categories for block types.
	 *
	 * @since 5.8.0
	 *
	 * @param array[]                 $block_categories     Array of categories for block types.
	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
	 */
	$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );

	if ( ! empty( $block_editor_context->post ) ) {
		$post = $block_editor_context->post;

		/**
		 * Filters the default array of categories for block types.
		 *
		 * @since 5.0.0
		 * @deprecated 5.8.0 Use the {@see 'block_categories_all'} filter instead.
		 *
		 * @param array[] $block_categories Array of categories for block types.
		 * @param WP_Post $post             Post being loaded.
		 */
		$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
	}

	return $block_categories;
}

常见问题

FAQs
查看更多 >