2014-05-06 15 views
0

我顯示一個側欄菜單列出父頁和子頁。

  • 家長
  • 子頁面一個
  • 兒童網頁在兩次
  • 兒童網頁放置三個

我的代碼是應用類.current_page_item到活動子頁面,但不與有源父頁面。我希望父母在頁面上也有類.current_page_item。

<?php 
if($post->post_parent) 
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); 
else 
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); 
if ($children) { 
    $parent_title = get_the_title($post->post_parent);?> 
    <ul id="sub"> 
     <li><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li> 
     <?php echo $children; ?> 
    </ul> 
<?php } ?> 

這裏是正在輸出什麼樣的想法:

<ul id="sub"> 
    <li><a href="#">Parent</a></li> 
    <li class="page_item page-item-19 current_page_item"><a href="#">Child page</a></li> 
    <li class="page_item page-item-21"><a href="#">Child page</a></li> 
    <li class="page_item page-item-23"><a href="#">Child page</a></li> 
</ul> 

回答

1

您應該使用get_pageswp_get_nav_menu_items。任何返回一個post對象數組的WordPress函數。

$children = wp_get_nav_menu_items('Main Menu'); // dump to make sure this is an array of objects 
echo "<ul id='sub'>"; 
foreach($children as $item) { 
    $class = ''; 
    if ($item->object_id == $post->post_parent){ 
     $class = 'current_page_ancestor'; 
    } else if ($item->object_id == $post->ID){ 
     $class = 'current_page_item'; 
    } 
    echo "<li " . $class . "><a href='" . $item->url . "'>" . $item->title . "</a></li>"; 
} 
echo "</ul>"; 

這得到一個WordPress的帖子數組,然後通過它們循環。

如果當前迭代中的帖子ID與您查看的帖子的父級相同,請添加類'current_page_ancestor。'。

如果當前迭代中的帖子ID與您查看的帖子相同,請添加類'current_page_item。'。

+0

謝謝,但是這個代碼似乎並沒有被返回任何東西! – xhadf

+0

你應該使用一個返回一個post對象數組的WordPress函數。 – pmandell