问题
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