我只会打印列表子类别。
例:
新闻 --> 新闻稿 --> 维京邮轮
我会打印维京游轮
我有这个代码可以工作,但打印整个类别树不是最后。
<?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 );
} ?>