最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to display list of child pages of parent page in wordpress?

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jul 3, 2014 at 11:57 Rarst 100k10 gold badges161 silver badges298 bronze badges asked Jul 3, 2014 at 11:40 Arshad HussainArshad Hussain 2101 gold badge5 silver badges17 bronze badges 1
  • Just a tip, please read the tag descriptions before using them. – Pieter Goosen Commented Jul 3, 2014 at 11:52
Add a comment  | 

2 Answers 2

Reset to default 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

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');
发布评论

评论列表(0)

  1. 暂无评论