本文作者:DurkBlue

WP调用显示父子分类目录详情推荐

WP调用显示父子分类目录详情摘要: WP调用显示父子分类目录,在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。对于自适应站点,特别的移动端,在分类页或文章页显示父子分类目录作为标签非常有用,...

WP调用显示父子分类目录,在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。对于自适应站点,特别的移动端,在分类页或文章页显示父子分类目录作为标签非常有用,效果如下:

WP调用显示父子分类目录详情 第1张

调用WP父子分类目录

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

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

  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. ?>

显示WP父子分类目录

当需要显示文章的父分类和当前分类链接时,我们可以使用这段代码:

  1. $categories = get_the_category();

  2. echo '<ul>';

  3. echo '<li> Parent Category: ';

  4. foreach$categories as $category ){

  5.      if($category->parent != 0){

  6.           $parent_category = get_term( $category->parent );

  7.           echo '<a href="' . esc_url( get_category_link($parent_category->term_id)) . '">' . esc_html($parent_category->name) . ' </a>';

  8.           break;

  9.      }

  10. }

  11. echo '</li>';

  12. echo '<li>Subcategory: ';

  13. foreach$categories as $category ){

  14.      if($category->parent != 0){

  15.           echo '<a href="' . esc_url( get_category_link($category->term_id)) . '">' . esc_html($category->name) . ' </a>';

  16.      }

  17. }

  18. echo '</li></ul>';

此篇文章由DurkBlue发布,撰文不易,转载请注明来处
文章投稿或转载声明

来源:DurkBlue版权归原作者所有,转载请保留出处。本站文章发布于 2020-02-27
温馨提示:文章内容系作者个人观点,不代表DurkBlue博客对其观点赞同或支持。

赞(0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论取消回复

快捷回复:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog

评论列表 (暂无评论,1642人围观)参与讨论

还没有评论,来说两句吧...