List Pages Sisters/Parents in Sidebar (2 posts)

  1. marcelolz
    Posted 1 year ago #


    How to add a list / menu in the sidebar to view the pages daughters and sisters of the current parent page?

    The code below displays the correct list when it is inserted on the page, but the widget it lists all pages , not just of the current group . Is there a way to change it to also work on the side ? Or perhaps another solution ?

    This page: http://www.cnbbsul4.org.br/projeto2014/para-familia/quando-ha-crise

    Thank you !

    hook :

    $submenu = hierarchical_submenu($post);
    if ($submenu) {
        echo $submenu;
    } else {
        // Do something else

    In Functions page:

    function hierarchical_submenu($post) {
        $top_post = $post;
        // If the post has ancestors, get its ultimate parent and make that the top post
        if ($post->post_parent && $post->ancestors) {
            $top_post = get_post(end($post->ancestors));
        // Always start traversing from the top of the tree
        return hierarchical_submenu_get_children($top_post, $post);
    function hierarchical_submenu_get_children($post, $current_page) {
        $menu = '';
        // Get all immediate children of this page
        $children = get_pages('child_of=' . $post->ID . '&parent=' . $post->ID . '&sort_column=menu_order&sort_order=ASC');
        if ($children) {
            $menu = "\n<ul>\n";
            foreach ($children as $child) {
                // If the child is the viewed page or one of its ancestors, highlight it
                if (in_array($child->ID, get_post_ancestors($current_page)) || ($child->ID == $current_page->ID)) {
                    $menu .= '<li class="sel"><a href="' . get_permalink($child) . '" class="sel"><strong>' . $child->post_title . '</strong></a>';
                } else {
                    $menu .= '<li><a href="' . get_permalink($child) . '">' . $child->post_title . '</a>';
                // If the page has children and is the viewed page or one of its ancestors, get its children
                if (get_children($child->ID) && (in_array($child->ID, get_post_ancestors($current_page)) || ($child->ID == $current_page->ID))) {
                    $menu .= hierarchical_submenu_get_children($child, $current_page);
                $menu .= "</li>\n";
            $menu .= "</ul>\n";
        return $menu;
  2. bcworkz
    Posted 1 year ago #

    The problem is once code execution gets to the sidebar, $post no longer has the current post object, so your code needs to get it through other means. You should verify this, but I believe the global $wp_query still represents the main query and your page's post. (unless some other code ran another query and neglected to use wp_reset_post_data())

    If so, you should be able to get the page's post object with $wp_query->get_queried_object(). However there's a bug in the code and it will only return a post object if pretty permalinks are on. It currently returns NULL with permalinks off. (Trac #27015). If you really need permalink off functionality, the patch in that ticket is an example of how to get the post object.

Topic Closed

This topic has been closed to new replies.

About this Topic