render_block_core_page_list

The timeline below displays how wordpress function render_block_core_page_list has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Renders the `core/page-list` block on server.
 *
 * @param array    $attributes The block attributes.
 * @param string   $content    The saved content.
 * @param WP_Block $block      The parsed block.
 *
 * @return string Returns the page list markup.
 */
function render_block_core_page_list($attributes, $content, $block)
{
    static $block_id = 0;
    ++$block_id;
    $parent_page_id = $attributes['parentPageID'];
    $is_nested = $attributes['isNested'];
    $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_queried_object_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), 'is_active' => $is_active);
        } else {
            $top_level_pages[$page->ID] = array('page_id' => $page->ID, 'title' => $page->post_title, 'link' => get_permalink($page), '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);
    if (0 !== $parent_page_id) {
        // If the parent page has no child pages, there is nothing to show.
        if (!array_key_exists($parent_page_id, $pages_with_children)) {
            return;
        }
        $nested_pages = block_core_page_list_nest_pages($pages_with_children[$parent_page_id], $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 = $is_nested ? '%2$s' : '<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, $is_nested, $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);
}

WordPress Version: 6.2

/**
 * Renders the `core/page-list` block on server.
 *
 * @param array    $attributes The block attributes.
 * @param string   $content    The saved content.
 * @param WP_Block $block      The parsed block.
 *
 * @return string Returns the page list markup.
 */
function render_block_core_page_list($attributes, $content, $block)
{
    static $block_id = 0;
    ++$block_id;
    $parent_page_id = $attributes['parentPageID'];
    $is_nested = $attributes['isNested'];
    $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_queried_object_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);
    if (0 !== $parent_page_id) {
        // If the parent page has no child pages, there is nothing to show.
        if (!array_key_exists($parent_page_id, $pages_with_children)) {
            return;
        }
        $nested_pages = block_core_page_list_nest_pages($pages_with_children[$parent_page_id], $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 = $is_nested ? '%2$s' : '<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, $is_nested, $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);
}

WordPress Version: 6.1

/**
 * Renders the `core/page-list` block on server.
 *
 * @param array    $attributes The block attributes.
 * @param string   $content    The saved content.
 * @param WP_Block $block      The parsed block.
 *
 * @return string Returns the page list markup.
 */
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);
}

WordPress Version: 5.9

/**
 * Renders the `core/page-list` block on server.
 *
 * @param array $attributes The block attributes.
 * @param array $content The saved content.
 * @param array $block The parsed block.
 *
 * @return string Returns the page list markup.
 */
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 thare 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);
    // Limit the number of items to be visually displayed.
    if (!empty($attributes['__unstableMaxPages'])) {
        $nested_pages = array_slice($nested_pages, 0, $attributes['__unstableMaxPages']);
    }
    $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);
}

WordPress Version: 5.8

/**
 * Renders the `core/page-list` block on server.
 *
 * @param array $attributes The block attributes.
 * @param array $content The saved content.
 * @param array $block The parsed block.
 *
 * @return string Returns the page list markup.
 */
function render_block_core_page_list($attributes, $content, $block)
{
    static $block_id = 0;
    $block_id++;
    // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby values is resolved,
    // update 'sort_column' to 'menu_order, post_title'. Sorting by both menu_order and post_title ensures a stable sort.
    // Otherwise with pages that have the same menu_order value, we can see different ordering depending on how DB
    // queries are constructed internally. For example we might see a different order when a limit is set to <499
    // versus >= 500.
    $all_pages = get_pages(array('sort_column' => 'menu_order', 'order' => 'asc'));
    $top_level_pages = array();
    $pages_with_children = array();
    foreach ((array) $all_pages as $page) {
        if ($page->post_parent) {
            $pages_with_children[$page->post_parent][$page->ID] = array('title' => $page->post_title, 'link' => get_permalink($page->ID));
        } else {
            $top_level_pages[$page->ID] = array('title' => $page->post_title, 'link' => get_permalink($page->ID));
        }
    }
    $nested_pages = block_core_page_list_nest_pages($top_level_pages, $pages_with_children);
    $wrapper_markup = '<ul %1$s>%2$s</ul>';
    $items_markup = block_core_page_list_render_nested_page_list($nested_pages);
    $colors = block_core_page_list_build_css_colors($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));
    if ($block->context && $block->context['showSubmenuIcon']) {
        $css_classes .= ' show-submenu-icons';
    }
    $wrapper_attributes = get_block_wrapper_attributes(array('class' => $css_classes, 'style' => $style_attribute));
    return sprintf($wrapper_markup, $wrapper_attributes, $items_markup);
}