显示最后一个子类别 [英] Display last child categories

查看:25
本文介绍了显示最后一个子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只会打印列表子类别.

I would print only the list child categories.

示例:

新闻 --> 新闻稿 --> 维京游轮

我会打印维京游轮

我有这个有效的代码,但不会打印整个类别树.

I have this code that works, but print the entire tree of categories not last.

<?php $categories = get_the_category();

      $separator = ' ';

      $output = '';

      if ( ! empty( $categories ) ) {

          foreach( $categories as $category ) {

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

          echo trim( $output, $separator );

      } ?>   

感谢您的帮助!

推荐答案

我已经修改了你的代码,请试试.希望它对你有用.

I have modified your code, Please try it. Hope it will work for you.

 <?php $categories = get_the_category();
     $separator = ' ';
     $output = '';
     if ( ! empty( $categories ) ) {
     foreach( $categories as $category ) {
      $children=get_categories(array( 'parent' => $category->cat_ID ));
          if ( count($children) == 0 ) {
            $output .= '<a href="' . esc_url( get_category_link( 
            $category->term_id ) ) . '">' . esc_html ($category->name) . 
           '</a>' . $separator;
         }
    }

         echo trim( $output, $separator );

  } ?>

这篇关于显示最后一个子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆