变化不大:
<?php $category_separator = "";
foreach((get_the_category()) as $childcat) {
$parentcat = $childcat->term_id;
if( $parentcat != 0 ) echo \'<span class="\' . get_cat_name($parentcat) .\'"><a href="\' . get_category_link($parentcat) . \'">\' .get_cat_name($parentcat) .\'</a></span>\';
else echo \'\' . $category_separator . \'<span class="\'. $childcat->cat_name .\'"><a href="\' . get_category_link($childcat) .\'">\' . $childcat->cat_name . \'</a></span>\';
$category_separator = " / ";
}
?>
实际上,您正在获取父类别id
$parentcat = $childcat->category_parent;
相反,您需要获取实际的子类别id
$parentcat = $childcat->term_id;