block_core_navigation_parse_blocks_from_menu_items

函数
block_core_navigation_parse_blocks_from_menu_items ( $menu_items, $menu_items_by_parent_id )
参数
  • (array) $menu_items An array of menu items that represent an individual level of a menu.
    Required:
  • (array) $menu_items_by_parent_id An array keyed by the id of the parent menu where each element is an array of menu items that belong to that parent.
    Required:
返回值
  • (array) An array of parsed block data.
定义位置
相关方法
block_core_navigation_sort_menu_items_by_parent_idblock_core_navigation_from_block_get_post_idsblock_core_navigation_build_css_font_sizesblock_core_navigation_render_submenu_iconblock_core_navigation_link_render_submenu_icon
引入
-
弃用
-

block_core_navigation_parse_blocks_from_menu_items: 这个函数用来解析导航菜单项,并将其转换为块编辑块。它为每个菜单项创建一个区块编辑器块,并为其分配相关属性。

将菜单项数据变成一个嵌套的解析块数组

function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) {
		if ( empty( $menu_items ) ) {
			return array();
		}

		$blocks = array();

		foreach ( $menu_items as $menu_item ) {
			$class_name       = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
			$id               = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
			$opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
			$rel              = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
			$kind             = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';

			$block = array(
				'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
				'attrs'     => array(
					'className'     => $class_name,
					'description'   => $menu_item->description,
					'id'            => $id,
					'kind'          => $kind,
					'label'         => $menu_item->title,
					'opensInNewTab' => $opens_in_new_tab,
					'rel'           => $rel,
					'title'         => $menu_item->attr_title,
					'type'          => $menu_item->object,
					'url'           => $menu_item->url,
				),
			);

			$block['innerBlocks']  = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
				? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
				: array();
			$block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );

			$blocks[] = $block;
		}

		return $blocks;
	}
}

常见问题

FAQs
查看更多 >