按层级输出自定义分类法的所有分类

已经多次有网友邮件提问,使用register_taxonomy注册了一个新的自定义分类法,比如“products”,然后在后台创建了分类,想在前台将这些分类按层级列出,怎么办。

本篇教程就教大家实现1. 按层级输出分类。2. 设置当前访问项

当前访问项

首先,按层级列出分类的时候,我们需要将当前访问的分类加上特殊标记,比如"active"、"current"。

思路:需要考虑三种页面, 1. 归档页,2.分类页面,3. 文章页。归档页是没有当前访问项的,所以不需要考虑,分类页面,不能简单的获取当前分类,若是在子分类页面,还需将父分类也定义为正在访问,文章页,也是一样,需要考虑父分类等。所以,正在访问的项可能不止一个。

用图来表示,可能更明了。

term_current

下面的代码,将当前访问项(可能是多个)放入数组$current_array 中,要实现还需使用到一个阿树自建的一个函数ashuwp_get_top_term_id。

  1. /**
  2. *获取任意分类的"顶级父分类",返回分类ID,若本身是顶级分类,返回本身ID
  3. * $term_id 分类的ID
  4. * $taxonomy 分类法,默认为category
  5. * $top_level 人为设定的顶级分类的父分类,默认为0。范例,若设置为3,则将ID为3的分类的第一级子分类定义"顶级父分类"
  6. **/
  7. function ashuwp_get_top_term_id ($term_id ,$taxonomy='category', $top_level=0) {
  8.     while ($term_id != $top_level) {
  9.         $term = get_term($term_id$taxonomy);
  10.         $term_id = $term->parent;
  11.         $parent_id = $term->term_id;
  12.     }
  13.     return $parent_id;
  14. }
  15. /**先获取当前页面的分类ID**/
  16. //分类页面
  17. if(is_tax('products')){
  18.   $currentterm = get_queried_object(); //获取当前分类
  19.   $currentterm_id = $currentterm->term_id;
  20.   //当前分类的顶级父分类ID
  21.   $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  22.   //获取顶级分类对象
  23.   $top_term = get_term($top_term_id,'products');
  24.   //当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  25.   $current_array = array($currentterm_id);
  26.   $parent_id = $currentterm->parent;
  27.   while($parent_id){
  28.     $current_array[] = $parent_id;
  29.     $parent_term = get_term($parent_id, 'products');
  30.     $parent_id = $parent_term->parent;
  31.   }
  32. }elseif(is_singular('product')){
  33.   //单页面
  34.   /*获取文章所属分类,文章同属多个分类,将第一个分类当做“当前分类”
  35.   *重复上面工作,将当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  36.   */
  37.   $terms = get_the_terms( $post->ID, 'products' );
  38.   if ( $terms && ! is_wp_error( $terms ) ) :
  39.     $currentterm = current($terms);
  40.     $current_array[] = $currentterm->term_id;
  41.     $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  42.     $top_term = get_term($top_term_id,'products');
  43.     $parent_id = $currentterm->parent;
  44.     while($parent_id){
  45.       $current_array[] = $parent_id;
  46.       $parent_term = get_term($parent_id, 'products');
  47.       $parent_id = $parent_term->parent;
  48.     }
  49.   else:
  50.     $currentterm_id = 0;
  51.     $top_term_id = 0;
  52.     $current_array = array();
  53.   endif;
  54. }else{
  55.   //若为归档页面,则没有当前访问
  56.   $currentterm_id = 0;
  57.   $top_term_id = 0;
  58.   $current_array = array();
  59. }

 

按层级输出

思路:学过任意一门变成语言的人应该都会这个思路,先获取所有顶级分类,将顶级分类循环输出,在循环内部再嵌套输出子分类的子循环。
  1. //先获取所有顶级分类
  2. $top_args = array(
  3.   'taxonomy'=>'products', //分类法名称
  4.   'hide_empty'=>false,
  5.   'parent'=>0, //顶级分类的父级都是0
  6. );
  7. $top_terms = get_terms($top_args);
  8. if ( $top_terms && ! is_wp_error( $top_terms ) ) :
  9.   echo '<ul class="top_ul">'; //最外层ul标签
  10.   //顶级分类循环输出
  11.   foreach$top_terms as $top_term ):
  12.     $current = '';
  13.     //若这个顶级分类的ID在数组$current_array中,为当前访问项
  14.     if(in_array($top_term->term_id,$current_array))
  15.       $current = 'class="current"';
  16.   ?>
  17.     <li <?php echo $current; ?>>
  18.       <a href="<?php echo get_term_link($top_term,'products'); ?>"><?php echo $top_term->name; ?></a>
  19.       <?php
  20.       //获取当前顶级分类的子分类
  21.       $child_args = array(
  22.         'taxonomy'=>'products',
  23.         'hide_empty'=>false,
  24.         'parent'=>$top_term->term_id,
  25.       );
  26.       $child_terms = get_terms($child_args);
  27.       if ( $child_terms && ! is_wp_error( $child_terms ) ) :
  28.         echo '<ul>'; //第二层ul标签
  29.         //循环子分类
  30.         foreach($child_terms as $child_term):
  31.           $current = '';
  32.           //若这个子分类的ID在数组$current_array中,为当前访问项
  33.           if(in_array($child_term->term_id,$current_array))
  34.             $current = 'class="current"';
  35.           ?>
  36.             <li <?php echo $current; ?>><a href="<?php echo get_term_link($child_term,'products'); ?>"><?php echo $child_term->name; ?></a></li>
  37.           <?php
  38.           endforeach;
  39.         echo '</ul>';
  40.       endif;
  41.       ?>
  42.     </li>
  43.   <?php
  44.   endforeach;
  45.   echo '</ul>';
  46. endif;

总结

本篇教程共写了两段代码,如果你并不清楚如何应用,请往下看。

首先,将本教程用到的一个函数放在主题的functions.php文件中,如下。

  1. /**
  2. *获取任意分类的"顶级父分类",返回分类ID,若本身是顶级分类,返回本身ID
  3. * $term_id 分类的ID
  4. * $taxonomy 分类法,默认为category
  5. * $top_level 人为设定的顶级分类的父分类,默认为0。范例,若设置为3,则将ID为3的分类的第一级子分类定义"顶级父分类"
  6. **/
  7. function ashuwp_get_top_term_id ($term_id ,$taxonomy='category', $top_level=0) {
  8.     while ($term_id != $top_level) {
  9.         $term = get_term($term_id$taxonomy);
  10.         $term_id = $term->parent;
  11.         $parent_id = $term->term_id;
  12.     }
  13.     return $parent_id;
  14. }

再将下面一整段代码放在要输出分类列表的地方。

  1. <?php
  2. //分类页面
  3. if(is_tax('products')){
  4.   $currentterm = get_queried_object(); //获取当前分类
  5.   $currentterm_id = $currentterm->term_id;
  6.   //当前分类的顶级父分类ID
  7.   $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  8.   //获取顶级分类对象
  9.   $top_term = get_term($top_term_id,'products');
  10.   //当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  11.   $current_array = array($currentterm_id);
  12.   $parent_id = $currentterm->parent;
  13.   while($parent_id){
  14.     $current_array[] = $parent_id;
  15.     $parent_term = get_term($parent_id, 'products');
  16.     $parent_id = $parent_term->parent;
  17.   }
  18. }elseif(is_singular('product')){
  19.   //单页面
  20.   /*获取文章所属分类,文章同属多个分类,将第一个分类当做“当前分类”
  21.   *重复上面工作,将当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  22.   */
  23.   $terms = get_the_terms( $post->ID, 'products' );
  24.   if ( $terms && ! is_wp_error( $terms ) ) :
  25.     $currentterm = current($terms);
  26.     $current_array[] = $currentterm->term_id;
  27.     $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  28.     $top_term = get_term($top_term_id,'products');
  29.     $parent_id = $currentterm->parent;
  30.     while($parent_id){
  31.       $current_array[] = $parent_id;
  32.       $parent_term = get_term($parent_id, 'products');
  33.       $parent_id = $parent_term->parent;
  34.     }
  35.   else:
  36.     $currentterm_id = 0;
  37.     $top_term_id = 0;
  38.     $current_array = array();
  39.   endif;
  40. }else{
  41.   //若为归档页面,则没有当前访问
  42.   $currentterm_id = 0;
  43.   $top_term_id = 0;
  44.   $current_array = array();
  45. }
  46. //先获取所有顶级分类
  47. $top_args = array(
  48.   'taxonomy'=>'products', //分类法名称
  49.   'hide_empty'=>false,
  50.   'parent'=>0, //顶级分类的父级都是0
  51. );
  52. $top_terms = get_terms($top_args);
  53. if ( $top_terms && ! is_wp_error( $top_terms ) ) :
  54.   echo '<ul class="top_ul">'; //最外层ul标签
  55.   //顶级分类循环输出
  56.   foreach$top_terms as $top_term ):
  57.     $current = '';
  58.     //若这个顶级分类的ID在数组$current_array中,为当前访问项
  59.     if(in_array($top_term->term_id,$current_array))
  60.       $current = 'class="current"';
  61.   ?>
  62.     <li <?php echo $current; ?>>
  63.       <a href="<?php echo get_term_link($top_term,'products'); ?>"><?php echo $top_term->name; ?></a>
  64.       <?php
  65.       //获取当前顶级分类的子分类
  66.       $child_args = array(
  67.         'taxonomy'=>'products',
  68.         'hide_empty'=>false,
  69.         'parent'=>$top_term->term_id,
  70.       );
  71.       $child_terms = get_terms($child_args);
  72.       if ( $child_terms && ! is_wp_error( $child_terms ) ) :
  73.         echo '<ul>'; //第二层ul标签
  74.         //循环子分类
  75.         foreach($child_terms as $child_term):
  76.           $current = '';
  77.           //若这个子分类的ID在数组$current_array中,为当前访问项
  78.           if(in_array($child_term->term_id,$current_array))
  79.             $current = 'class="current"';
  80.           ?>
  81.             <li <?php echo $current; ?>><a href="<?php echo get_term_link($child_term,'products'); ?>"><?php echo $child_term->name; ?></a></li>
  82.           <?php
  83.           endforeach;
  84.         echo '</ul>';
  85.       endif;
  86.       ?>
  87.     </li>
  88.   <?php
  89.   endforeach;
  90.   echo '</ul>';
  91. endif;
  92. ?>

The end.

本篇教程之前的几篇教程是

本篇教程之后的几篇教程是

没有找到你要找的内容?你可以通过搜索你要找的内容,或者给我们留言。

已有0评论

暂时木有评论.

发表评论