3

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.

1
  • Just a tip, please read the tag descriptions before using them. Commented Jul 3, 2014 at 11:52

2 Answers 2

3

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

function wpb_list_child_pages() { 

    global $post; 

    $id = ( is_page() && $post->post_parent ) ? $post->post_parent : $post->ID;
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $id . '&echo=0' );
    //you can add `&depth=1` in the end, so it only shows one level

    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.

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

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

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 and acknowledge you have read our privacy policy.

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