Display last child categories

旧巷老猫 提交于 2019-12-13 03:33:15

问题


I would print only the list child categories.

Example :

News --> Press Release --> Viking Cruises

I would print Viking Cruises

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

      } ?>   

thanks for help!


回答1:


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

  } ?>


来源:https://stackoverflow.com/questions/49168105/display-last-child-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!