首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress网站导航

wordpress网站导航
EN

Stack Overflow用户
提问于 2011-03-11 20:55:47
回答 2查看 364关注 0票数 0

我使用下面的代码来显示我的3级菜单:

代码语言:javascript
复制
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 } ?>

它在侧边栏中列出页面,第二级,然后第三级。我想包括非常顶级的水平,所以我想我的结构看起来如下:

代码语言:javascript
复制
*A
 -a
  --a
 -b
  --b
 -c
  --c

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

谢谢,

EN

回答 2

Stack Overflow用户

发布于 2011-03-11 21:36:13

我在wordpress codex site找到了this code snippet,我想这正是你要找的,为了方便,我把它贴进去了:

代码语言:javascript
复制
<?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 } ?>

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

票数 0
EN

Stack Overflow用户

发布于 2012-03-08 11:20:28

代码语言:javascript
复制
<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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5273133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档