【问题标题】:wordpress site navigationwordpress 网站导航
【发布时间】:2011-03-11 12:55:46
【问题描述】:

我正在使用以下代码来显示我的 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 } ?>

它在侧栏中列出页面,第二级然后第三级。我也想包括非常顶级的,所以我希望我的结构如下所示:

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

上面的代码没有列出主页,即*A,我希望这是有道理的,有人能够提供帮助

谢谢,

【问题讨论】:

  • 你能澄清一下吗?如果您在页面--a 上,您想在侧边栏中显示什么?如果您在页面 -b 上,您希望在侧边栏中显示什么?

标签: php wordpress


【解决方案1】:

我在wordpress codex site 找到了this code snippet,我认为这正是您要查找的内容,为方便起见,我已将其粘贴:

<?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 } ?>

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

【讨论】:

  • 几乎,但是当我在首页时它没有显示第三级。因此,按照我的示例逻辑,当我在 *A 上时,它只显示 -a -b -c 而当我在 -a 时,它只显示 *A -a --a
  • @jmysona 你所要做的就是改变深度。如果您查看else 子句中的行,只需将其更改为:wp_list_pages("title_li=&amp;echo=0&amp;depth=2&amp;child_of=".$post-&gt;ID);
【解决方案2】:
<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多