render_block_core_page_list

函数
render_block_core_page_list ( $attributes, $content, $block )
参数
  • (array) $attributes The block attributes.
    Required:
  • (string) $content The saved content.
    Required:
  • (WP_Block) $block The parsed block.
    Required:
返回值
  • (string) Returns the page list markup.
定义位置
相关方法
register_block_core_page_listrender_block_core_imagerender_block_core_tag_cloudrender_block_core_home_linkrender_block_core_pattern
引入
-
弃用
-

render_block_core_page_list: 这个函数是用来渲染WordPress中的Page List块的HTML内容。页面列表块允许用户在他们的网站上显示一个页面列表,这个函数负责生成该列表的HTML标记。

在服务器上渲染`core/page-list’区块。

function render_block_core_page_list( $attributes, $content, $block ) {
	static $block_id = 0;
	$block_id++;

	$all_pages = get_pages(
		array(
			'sort_column' => 'menu_order,post_title',
			'order'       => 'asc',
		)
	);

	// If there are no pages, there is nothing to show.
	if ( empty( $all_pages ) ) {
		return;
	}

	$top_level_pages = array();

	$pages_with_children = array();

	$active_page_ancestor_ids = array();

	foreach ( (array) $all_pages as $page ) {
		$is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID );

		if ( $is_active ) {
			$active_page_ancestor_ids = get_post_ancestors( $page->ID );
		}

		if ( $page->post_parent ) {
			$pages_with_children[ $page->post_parent ][ $page->ID ] = array(
				'page_id'   => $page->ID,
				'title'     => $page->post_title,
				'link'      => get_permalink( $page->ID ),
				'is_active' => $is_active,
			);
		} else {
			$top_level_pages[ $page->ID ] = array(
				'page_id'   => $page->ID,
				'title'     => $page->post_title,
				'link'      => get_permalink( $page->ID ),
				'is_active' => $is_active,
			);

		}
	}

	$colors          = block_core_page_list_build_css_colors( $attributes, $block->context );
	$font_sizes      = block_core_page_list_build_css_font_sizes( $block->context );
	$classes         = array_merge(
		$colors['css_classes'],
		$font_sizes['css_classes']
	);
	$style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
	$css_classes     = trim( implode( ' ', $classes ) );

	$nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );

	$is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );

	$open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false;

	$show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;

	$wrapper_markup = '<ul %1$s>%2$s</ul>';

	$items_markup = block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $active_page_ancestor_ids, $colors );

	$wrapper_attributes = get_block_wrapper_attributes(
		array(
			'class' => $css_classes,
			'style' => $style_attribute,
		)
	);

	return sprintf(
		$wrapper_markup,
		$wrapper_attributes,
		$items_markup
	);
}

常见问题

FAQs
查看更多 >