2

I have following format of menu.

Treatment
 -Beauty
  --Services
  --Products
 -Surgery
 -washing

I have followed follwed following link: How to Display a List of Child Pages For a Parent Page in WordPress

But I do not want subpages of "Beauty" page. I want only "Treatment" Subpages.

  • Just a tip, please read the tag descriptions before using them. – Pieter Goosen Jul 3 '14 at 11:52
2

Add this code to your functions.php. An explanation of the code is given below.

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');

Explanation

The code checks to see if a page has a parent or the page itself is a parent. If it is a parent page, then it displays the child pages associated with it. If it is a child page, then it displays all other child pages of its parent page. Lastly, if this is just a page with no child or parent page, then the code will simply do nothing. So just add this shortcode [wpb_childpages] to the page where it's child pages will be displayed.

Update to stop at first level

if ( is_page() && $post->post_parent )    
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0&depth=1' );
else
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0&depth=1' );

My test output that worked at my localhost:

Test1

-t1

--tt1

-t2

and the output that displays when I wrote that shortcode in Test1 page is:

t1

t2

  • 1
    But it is also fetching subpages of subpages. I want only subpages of parent page. – Arshad Hussain Jul 3 '14 at 11:53
  • @ArshadHussain: ok.. then for that add &depth=1 with &echo.. see my updated line in my post. – Zammuuz Jul 3 '14 at 11:58
  • not working yet. – Arshad Hussain Jul 3 '14 at 12:09
  • what is showing? still services,products are showing?? but it worked for me.. – Zammuuz Jul 3 '14 at 12:11
  • 1
    walah, it is working.. – Arshad Hussain Jul 3 '14 at 12:37
0

If you only want subpages of the current page then use this:

function wpb_list_child_pages() {

    global $post; 

if ( is_page() && $post->ID )    
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0&depth=2' );

    if ( $childpages ) {    
         $string = '<ul>' . $childpages . '</ul>';
    }

    return $string;
}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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