显示最后一个子类别

Display last child categories

我只会打印子类别列表。

示例:

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

我会打印 Viking Cruises

我有这个代码可以工作,但打印整个类别树不是最后。

<?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 );

      } ?>   

感谢帮助!

我修改了你的代码,请试试。希望对你有用。

 <?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 );

  } ?>