调用WordPress父子分类目录

WordPress119.1K阅读模式

在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。

代码一:

将下面代码加到主题模板适当位置,比如侧边栏:

  1. <?php
  2.     $current = "";
  3.     if(is_single()){
  4.         $parent = get_the_category();
  5.         $parent = $parent[0];
  6.         $current = "&current_category=".$parent->term_id;
  7.     }else if(is_category()){
  8.         global $cat;
  9.         $parent = get_category($cat);
  10.     }
  11.     if($parent->category_parent != 0){
  12.         $cat_id = $parent->category_parent;
  13.         $parent = get_category($cat_id);
  14.         if($parent->category_parent != 0){
  15.             $cat_id = $parent->category_parent;
  16.         }else{
  17.             $cat_id = $parent->term_id;
  18.         }
  19.     }else{
  20.         $cat_id = $parent->term_id;
  21.     }
  22. ?>
  23. <?php if(!is_page()) { ?>
  24.     <h3><?php echo $parent->cat_name; ?><span><?php echo $parent->slug; ?></span></h3>
  25.     <ul id="cat_list">
  26.         <?php wp_list_categories("title_li=&child_of=$cat_id".$current); ?>   
  27.     </ul>
  28. <?php } ?>

代码二:

将下面代码加到主题function.php模板文件中:

  1. function get_category_root_id($cat)
  2. {
  3.     $this_category = get_category($cat); // 取得当前分类
  4.     while($this_category->category_parent) // 若当前分类有上级分类时,循环
  5.     {
  6.         $this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
  7.     }
  8.     return $this_category->term_id; // 返回根分类的id号
  9. }

调用显示代码加到主题模板的适当位置:

  1. <?php
  2.     if(is_category())
  3.     {
  4.         if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
  5.         {
  6.             echo '<ul>';
  7.             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");
  8.             echo '</ul>';
  9.         }
  10.     }
  11. ?>

 

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  11  访客  11
    • 两天
      两天 6

      用不到,来支持下

      • 第九部落
        第九部落 1

        不错啊,鸟哥!还是鸟哥的东西实用!

        • vishay电阻
          vishay电阻 0

          我的网站正需此功能啊,太感谢了

          • 大肥羊
            大肥羊 4

            这货会被adblock咔嚓掉。。。

            • 邪风
              邪风 2

              非常感激博主,这个对我很有用,太好了谢谢

              • 心语
                心语 2

                知更鸟,您好!请问下网站下某个分类下有子分类,怎么样可以在标题中这样实现:标题_分类_子分类_网站名称,谢谢!

                • jeepgge
                  jeepgge 6

                  鸟兄,我侧边的主题自带的分类目录里面怎么点击没反应,原来里面有显示每个分类目录,现在不显示了为什么啊?

                  • jeepgge
                    jeepgge 6

                    就是主题自带的,在右上边,近期热门,最受欢迎,分类目录,就这个分类目录不显示下面的分类,不知道为什么!麻烦指点一下

                    • 4
                      4 0

                      怎么添加 判断

                      • 天涯共此时
                        天涯共此时 0

                        有用,我试一下。

                      匿名

                      发表评论

                      匿名网友
                      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                      拖动滑块以完成验证