Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have following format of sub-pages in WordPress:

About Us 
  -Services 
  -Products 
  -Surgery 

I want to list all sub-pages with parent page like above but the code that I am using

from wpbeginner site is not working as my requirement. Can somebody tell me that

how to show sub-pages with parent page.

I tried following code in functions.php file:

function wpb_list_child_pages() {
    global $post;
    if ( is_page() && $post->post_parent ) {
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    } else {
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    }
    if ( $childpages ) {
        $string = '<ul>' . $childpages . '</ul>';
    }

    return $string;
}    
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );

And then use short code [wpb_childpages] in widget but it is showing only child page but I want child page with parent page

share|improve this question
    
What do you mean by not meeting your requirements. Have you tried anything to make this code meet your requirements? Where have you failed? –  Pieter Goosen Jul 9 at 7:43
    
@PieterGoosen yes, Arshad! you have to try it. Then find the reason why it's not working to you. Also, provide us what have you try? –  Big Bro Yesh Jul 9 at 7:51
    
Give your code which you have tried so far. –  Bindiya Patoliya Jul 9 at 7:52
    
I updated my question, please see. –  Arshad Hussain Jul 9 at 7:54

1 Answer 1

up vote 0 down vote accepted

The code you are using only gets the children of a parent page. It first checks to see in which page you are. If you are in a child page it uses the parent ID to print it's children. If you're in a parent page then it just looks for it's children using the post ID. Neither case it prints the actual parent. I edited the code so that it will print the parent. If you're in a child page it will get the parent using get_the_title(), if you're in a parent page then you already have that information in the $post object. See if that works for you.

function wpb_list_child_pages() {
    global $post;
    $parent = "";
    if ( is_page() && $post->post_parent ) {
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
        $parent =  get_the_title($post->post_parent);
    } else {
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

        $parent = $post->post_title;
    }
    if ( $childpages ) {
        $string = '<ul><li>' . $parent . ';
        $string .= '<ul>' . $childpages . '</ul>';
        $string .= '</li></ul>';
    }

    return $string;
}    
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );

Alternatively you can also make the parent title a link by using get_permalink().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.