当前位置:蚂蚁分类目录 » 站长资讯 » 站长运营 » WordPress教程 » 文章详细 订阅RssFeed

wordpress调用当前分类的全部子分类方法 简单几句代码解决

来源:蚂蚁目录 浏览:841次 时间:2021-04-28 14:01:25

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

首先在主题的“functions.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. }

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

  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)).
  7. "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
  8. }
  9. }
  10. ?>

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