wordpress网站导航

时间:2011-03-11 12:55:47

标签: php wordpress

我正在使用以下代码显示我的3级菜单:

if(!$post->post_parent){
   // will display the subpages of this top level page
   $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
    // diplays only the subpages of parent level
   //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

   if($post->ancestors) {
        // now you can get the the top ID of this page
        // wp is putting the ids DESC, thats why the top level ID is the last one
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        // you will always get the whole subpages list
    }
}

if ($children) { ?>
    <ul id="submenu">
        <?php echo $children; ?>
    </ul>
<?php } ?>

它会在侧栏中列出页面,第二级也会在第3级列出页面。我想包括非常顶级,所以我希望我的结构看起来如下:

*A
 -a
  --a
 -b
  --b
 -c
  --c

如果上面的代码没有列出主页,即* A,我希望有意义并且有人能够提供帮助

谢谢,

2 个答案:

答案 0 :(得分:0)

我在this code snippet找到了wordpress codex site,我认为这正是你要找的东西,为方便起见我贴了它:

<?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); // <---- THIS IS INCLUDING THE PARENT
  //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=2&child_of=".$post->ID);
}

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

如果这是最高级别的页面,它还有一些内置逻辑不显示所有内容。希望这有帮助!

答案 1 :(得分:0)

<div id="breadcrumbs">
  <a href="<?php echo get_bloginfo('url'); ?>" title="">Home</a>
  <?php
    $parent_id  = $post->post_parent;
    $breadcrumbs = array();
    while ($parent_id) {
      $page = get_page($parent_id);
      $breadcrumbs[] = '<a href="'.get_permalink($page->ID).'" title="">'.get_the_title($page->ID).'</a>';
      $parent_id  = $page->post_parent;
    }
    $breadcrumbs = array_reverse($breadcrumbs);
    foreach ($breadcrumbs as $crumb) echo ' / '.$crumb;
  ?>
</div>
<h1><?php the_title(); ?></h1>
信用:伊沃维奇 来自:http://wordpress.org/support/topic/display-page-parent-on-page-with-title?replies=13