0

我正在尝试使用 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"

感谢任何帮助!

4

1 回答 1

0

您可以使用wp_list_pages() 的exclude_tree和参数depth

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

于 2013-09-15T15:00:12.607 回答