wp_list_pages,隐藏没有祖先的父级,并向选定的帖子显示子级

时间:2013-09-15 14:25:21

标签: php wordpress

我正在尝试使用wp_list_pages创建菜单,我希望最终结果如下所示:

parent ( no ancestor ) <-- This one should be hidden
  - Children "Some Title"( clicked )
    - Children1 to "Some Title"
    - Children2 to "Some Title"

  - Children "Some other title" ( When clicking on this, children to "Somte Title" will hide and children to "Some other title" show )

我正在使用以下代码,我无法弄清楚如何修改它以给我想要的结果。

<?php
//if the post has a parent
if($post->post_parent){
    //collect ancestor pages
    $relations = get_post_ancestors($post->ID);

    //get child pages
    $result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
    if ($result){
        foreach($result as $pageID){
            array_push($relations, $pageID->ID);
        }
    }

    //add current post to pages
    array_push($relations, $post->ID);
    //get comma delimited list of children and parents and self
    $relations_string = implode(",",$relations);

    //use include to list only the collected pages.
    $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string); 
}else{
    // display only main level and children

    $sidelinks = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID);
}

if ($sidelinks) { ?>
    <?php echo the_title(); ?>
    <ul>
        <?php //links in <li> tags
        echo $sidelinks;
        ?>

    </ul>
<?php } ?>

上面的例子给我一个看起来像这样的布局,它显示没有祖先的父级,而不是被点击的子级的兄弟:

parent ( no ancestor )
  - Children "Some Title"( clicked )
    - Children1 to "Some Title"
    - Children2 to "Some Title"

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用wp_list_pages()的exclude_treedepth参数

有关详细信息,请参阅Wordpress Codex:http://codex.wordpress.org/Function_Reference/wp_list_pages