2015-02-07 95 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; 

这是做它的一种方式。还有其他的方法。我可以为您介绍这些功能:

相关问题