2015-02-07 92 views
1

我可以看到已經有很多關於此的信息,但我似乎無法找到任何最新的信息,只是想知道是否有人可以幫助我。wordpress在父類別中顯示子類別

我有不同的父類別和子類別,例如:

虛擬主機

  • 評論

域名註冊商

  • 頂REGIST RARS
  • 優惠碼

我使用的category.php頁面下面的代碼,並顯示子類在每個類別罰款:

<?php 
if (is_category()) { 
$this_category = get_category($cat); 
if($this_category->category_parent): 
else: 
$this_category = wp_list_categories('orderby=id&depth=5&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0"); 
echo '<ul>'. $this_category . '</ul>'; 
endif; 
} 
?> 

但是當我點擊一個子 - 分類鏈接它顯示該子類別中的所有帖子罰款,但那麼顯然沒有回到父目錄等鏈接等。

有沒有反正做這個?有沒有人有我的代碼沒有任何錯誤?非常感謝。

回答

0

您可以修改代碼以這樣的事:

<?php 

if (is_category()) : 
    $category = get_category($cat); 
    if ($category->category_parent) : // if category has parent 
     $category_parent_id = $category->category_parent; 
     $category_parent_link = get_category_link($category_parent_id); 
     echo '<a href="' . $category_parent_link . '">' . get_category($category_parent_id)->name . '</a>'; 
    else : // else category has children 
     $children = wp_list_categories(array(
      'child_of' => $category->cat_ID, 
      'depth' => 5, 
      'echo'  => 0, 
      'orderby' => 'id', 
      'title_li' => '', 
     )); 
     echo '<ul>' . $children . '</ul>'; 
    endif; 
endif; 

這是做它的一種方式。還有其他的方法。我可以爲您介紹這些功能:

相關問題