wordpress调用当前分类的全部子分类

wordpress在制作企业主题的时候比较常遇到要调用当前分类下的全部子分类。要实现这个可以按照以下步骤操作。

首先在主题的“functions.php”里面写个函数,代码如下:

PHP代码
  1. // 获取子分类  
  2. function get_category_root_id($cat){  
  3.     $this_category = get_category($cat);  
  4.     while($this_category->category_parent) {  
  5.         $this_category = get_category($this_category->category_parent);  
  6.     }  
  7.     return $this_category->term_id;  
  8. }  

然后在需要调用的页面里写以下代码:

PHP代码
  1. <?php  
  2. if(is_category()) {  
  3.     $cat = get_query_var('cat');  
  4.     $categoryurl = get_category_link($cat);  
  5.     if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" ) {  
  6.         echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");  
  7.     }  
  8. }  
  9. ?>  

最后自己调整下css就ok了。



评论: 0 | 引用: 0 | 查看次数: 152
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑