在子页面上列出包含所有子项的父项

时间:2019-03-25 作者:matstil

如何在WP中列出父项和子项?

我用这个:

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

but I need this:

<ul>
<li><a href="#">PARENT ITEM</a></li>
<li><a href="#">CHILD ITEM 1</a></li>
<li><a href="#">CHILD ITEM 2</a></li>
<li><a href="#">CHILD ITEM 3</a></li>
<li><a href="#">CHILD ITEM 4</a></li>
</ul>

1 个回复
SO网友:Qaisar Feroz
function wpb_list_child_pages() {
    global $post;
    $parent = "";
    if ( is_page() && $post->post_parent ) {
        $parent = wp_list_pages( \'title_li=&include=\' . $post->post_parent . \'&echo=0\' );
        $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\' );
        //$parent = $post->post_title;
    }

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

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

OUTPUT ON TOP LEVEL PAGE

<ul>
    <li class="page_item page-item-283">
       <a href="http://192.168.8.200/www/parent-page/child-page-1/">Child Page 1</a>
    </li>


    <li class="page_item page-item-285">
       <a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
    </li>

    <li class="page_item page-item-286">
       <a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
    </li>

</ul>

OUTPUT ON A CHILD PAGE

<ul>

    <li class="page_item page-item-282 current_page_ancestor current_page_parent">
       <a href="http://192.168.8.200/www/parent-page/">Parent page</a>
    </li>

    <li class="page_item page-item-283 current_page_item">
       <a href="http://192.168.8.200/www/parent-page/child-page-1/" aria-current="page">Child Page 1</a>
    </li>

    <li class="page_item page-item-285">
       <a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
   </li>

   <li class="page_item page-item-286">
       <a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
   </li>

</ul>

相关推荐

Add extra markup to WP menus

我被要求将一个HTML网站重新开发为一个WP主题,它有一个非常高级的菜单结构,我不太确定如何复制它。第二个菜单项有一个子菜单,但它不仅仅是“li”中的“ul”,还有额外的div等。。因为下拉列表有3列布局,其中2列包含子链接,第3列包含内容。这是一个示例:<ul class=\"nav navbar-nav three\"> <li class=\"dropdown yamm-fw\"> <a href=\"#\" class=\"dropdown-toggl