[WordPress教程] 按层级输出自定义分类法的所有分类 -静鱼客栈

2021-10-09 0 523

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

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

当前访问项

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

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

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


/**
*获取任意分类的"顶级父分类",返回分类ID,若本身是顶级分类,返回本身ID
* $term_id 分类的ID
* $taxonomy 分类法,默认为category
* $top_level 人为设定的顶级分类的父分类,默认为0。范例,若设置为3,则将ID为3的分类的第一级子分类定义"顶级父分类"
**/
function ashuwp_get_top_term_id ($term_id ,$taxonomy='category', $top_level=0) {
    while ($term_id != $top_level) {
        $term = get_term($term_id, $taxonomy);
        $term_id = $term->parent;
        $parent_id = $term->term_id;
    }
    return $parent_id;
}
/**先获取当前页面的分类ID**/
//分类页面
if(is_tax('products')){
  $currentterm = get_queried_object(); //获取当前分类
  $currentterm_id = $currentterm->term_id;
  //当前分类的顶级父分类ID
  $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  //获取顶级分类对象
  $top_term = get_term($top_term_id,'products');
  //当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  $current_array = array($currentterm_id);
  $parent_id = $currentterm->parent;
  while($parent_id){
    $current_array[] = $parent_id;
    $parent_term = get_term($parent_id, 'products');
    $parent_id = $parent_term->parent;
  }
}elseif(is_singular('product')){
  //单页面
  /*获取文章所属分类,文章同属多个分类,将第一个分类当做“当前分类”
  *重复上面工作,将当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  */
  $terms = get_the_terms( $post->ID, 'products' );
  if ( $terms && ! is_wp_error( $terms ) ) :
    $currentterm = current($terms);
    $current_array[] = $currentterm->term_id;
    $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
    $top_term = get_term($top_term_id,'products');
    $parent_id = $currentterm->parent;
    while($parent_id){
      $current_array[] = $parent_id;
      $parent_term = get_term($parent_id, 'products');
      $parent_id = $parent_term->parent;
    }
  else:
    $currentterm_id = 0;
    $top_term_id = 0;
    $current_array = array();
  endif;
}else{
  //若为归档页面,则没有当前访问
  $currentterm_id = 0;
  $top_term_id = 0;
  $current_array = array();
}

按层级输出

思路:学过任意一门变成语言的人应该都会这个思路,先获取所有顶级分类,将顶级分类循环输出,在循环内部再嵌套输出子分类的子循环。


//先获取所有顶级分类
$top_args = array(
  'taxonomy'=>'products', //分类法名称
  'hide_empty'=>false,
  'parent'=>0, //顶级分类的父级都是0
);
$top_terms = get_terms($top_args);
if ( $top_terms && ! is_wp_error( $top_terms ) ) :
  echo '<ul class="top_ul">'; //最外层ul标签
  //顶级分类循环输出
  foreach( $top_terms as $top_term ):
    $current = '';
    //若这个顶级分类的ID在数组$current_array中,为当前访问项
    if(in_array($top_term->term_id,$current_array))
      $current = 'class="current"';
  ?>
    <li <?php echo $current; ?>>
      <a href="<?php echo get_term_link($top_term,'products'); ?>" rel="external nofollow"  rel="external nofollow" ><?php echo $top_term->name; ?></a>
      <?php
      //获取当前顶级分类的子分类
      $child_args = array(
        'taxonomy'=>'products',
        'hide_empty'=>false,
        'parent'=>$top_term->term_id,
      );
      $child_terms = get_terms($child_args);
      if ( $child_terms && ! is_wp_error( $child_terms ) ) :
        echo '<ul>'; //第二层ul标签
        //循环子分类
        foreach($child_terms as $child_term):
          $current = '';
          //若这个子分类的ID在数组$current_array中,为当前访问项
          if(in_array($child_term->term_id,$current_array))
            $current = 'class="current"';
          ?>
            <li <?php echo $current; ?>><a href="<?php echo get_term_link($child_term,'products'); ?>" rel="external nofollow"  rel="external nofollow" ><?php echo $child_term->name; ?></a></li>
          <?php
          endforeach;
        echo '</ul>';
      endif;
      ?>
    </li>
  <?php
  endforeach;
  echo '</ul>';
endif;

总结

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

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


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

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


<?php
//分类页面
if(is_tax('products')){
  $currentterm = get_queried_object(); //获取当前分类
  $currentterm_id = $currentterm->term_id;
  //当前分类的顶级父分类ID
  $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
  //获取顶级分类对象
  $top_term = get_term($top_term_id,'products');
  //当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  $current_array = array($currentterm_id);
  $parent_id = $currentterm->parent;
  while($parent_id){
    $current_array[] = $parent_id;
    $parent_term = get_term($parent_id, 'products');
    $parent_id = $parent_term->parent;
  }
}elseif(is_singular('product')){
  //单页面
  /*获取文章所属分类,文章同属多个分类,将第一个分类当做“当前分类”
  *重复上面工作,将当前分类ID、当前分类的父分类ID都是当前访问,放入$current_array数组
  */
  $terms = get_the_terms( $post->ID, 'products' );
  if ( $terms && ! is_wp_error( $terms ) ) :
    $currentterm = current($terms);
    $current_array[] = $currentterm->term_id;
    $top_term_id = ashuwp_get_top_term_id($currentterm->term_id,'products');
    $top_term = get_term($top_term_id,'products');
    $parent_id = $currentterm->parent;
    while($parent_id){
      $current_array[] = $parent_id;
      $parent_term = get_term($parent_id, 'products');
      $parent_id = $parent_term->parent;
    }
  else:
    $currentterm_id = 0;
    $top_term_id = 0;
    $current_array = array();
  endif;
}else{
  //若为归档页面,则没有当前访问
  $currentterm_id = 0;
  $top_term_id = 0;
  $current_array = array();
}
//先获取所有顶级分类
$top_args = array(
  'taxonomy'=>'products', //分类法名称
  'hide_empty'=>false,
  'parent'=>0, //顶级分类的父级都是0
);
$top_terms = get_terms($top_args);
if ( $top_terms && ! is_wp_error( $top_terms ) ) :
  echo '<ul class="top_ul">'; //最外层ul标签
  //顶级分类循环输出
  foreach( $top_terms as $top_term ):
    $current = '';
    //若这个顶级分类的ID在数组$current_array中,为当前访问项
    if(in_array($top_term->term_id,$current_array))
      $current = 'class="current"';
  ?>
    <li <?php echo $current; ?>>
      <a href="<?php echo get_term_link($top_term,'products'); ?>" rel="external nofollow"  rel="external nofollow" ><?php echo $top_term->name; ?></a>
      <?php
      //获取当前顶级分类的子分类
      $child_args = array(
        'taxonomy'=>'products',
        'hide_empty'=>false,
        'parent'=>$top_term->term_id,
      );
      $child_terms = get_terms($child_args);
      if ( $child_terms && ! is_wp_error( $child_terms ) ) :
        echo '<ul>'; //第二层ul标签
        //循环子分类
        foreach($child_terms as $child_term):
          $current = '';
          //若这个子分类的ID在数组$current_array中,为当前访问项
          if(in_array($child_term->term_id,$current_array))
            $current = 'class="current"';
          ?>
            <li <?php echo $current; ?>><a href="<?php echo get_term_link($child_term,'products'); ?>" rel="external nofollow"  rel="external nofollow" ><?php echo $child_term->name; ?></a></li>
          <?php
          endforeach;
        echo '</ul>';
      endif;
      ?>
    </li>
  <?php
  endforeach;
  echo '</ul>';
endif;
?>

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝/QQ扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!305582964@qq.com
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有U点奖励和额外收入!

静鱼客栈 学习中心 [WordPress教程] 按层级输出自定义分类法的所有分类 -静鱼客栈 https://www.52jyu.cn/10626.html

静鱼客栈的帅逼站长~

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP贵宾介绍。
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器没有下载完整造成的,可以重新进行下载即可~
查看详情

相关文章

发表评论
暂无评论
静鱼客栈-站长

为您解决烦忧 - 专业服务 看到消息会进行回复