2011-03-11 128 views
0

我用下面的代碼,以顯示我的3級菜單:WordPress站點導航

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,我希望實際意義,有人會能夠幫助

感謝,

+0

你能否澄清?如果你在頁面上--a,你想在邊欄中顯示什麼?如果你在頁面-b上,你想在邊欄中顯示什麼? – Groovetrain 2011-03-11 18:37:22

回答

0

我發現this code snippetwordpress 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 } ?> 

它也有一些內置邏輯,如果這是最高級別的頁面,則不顯示所有內容。希望這可以幫助!

+0

謝謝Groovetrain – jmysona 2011-03-11 13:46:07

+0

幾乎,但是它沒有顯示第三級,當我在首頁。 所以效法我的邏輯。當我在* A公司只顯示 -a -b -c ,當我在-a其只顯示 * A -a --a – jmysona 2011-03-11 17:42:31

+0

@ jmysona所有你需要做的就是改變深度。如果您查看'else'子句中的行,只需將其更改爲:'wp_list_pages(「title_li =&echo = 0&depth = 2&child_of =」。$ post-> ID);' – Groovetrain 2011-03-11 17:45:39

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> 

信用:Ivovic 來自:http://wordpress.org/support/topic/display-page-parent-on-page-with-title?replies=13