WordPress Sub Menu / Show Sister Pages

I am currently using this snippet to show children of the current page.

<?php wp_list_pages('title_li=&child_of='.$post->ID.''); ?>

when I click one of those children pages the sub nav disappears because there are no child pages of the current page, so what I need it to do is continue to show the sister pages of the current page.

Read More

What is the best way to do this?

Related posts

1 comment

  1. You could request the parent ID instead of the post ID if there is no childs on the current page :

    $page_query = new WP_Query();
    $all_pages = $page_query->query(array('post_type' => 'page'));
    $childs = get_page_children($post-ID, $all_pages);
    if(!empty($childs)) {
        // display the page childs
        wp_list_pages($post->ID);
    } else {
        // display the page siblings
        wp_list_pages(wp_get_post_parent_id($post->ID));
    }
    

    get_page_children is used to check if there is any child for the current page – the WP_Query part is needed for get_page_children which required an array of all the pages to look in.

Comments are closed.