这里有一个函数,它从帖子中获取类别,然后显示一个无序的术语子链接列表。我假设你每个帖子只有一个类别。如果没有,则必须迭代$cat_id
除了$cat_children
:
// get category id
$cat_id = get_the_category();
// get array of all children of the first category assigned to the post
$cat_children = get_term_children( $cat_id[0]->term_id, \'category\' );
// start building our list
$cat_children_list = \'<ul>\';
foreach( $cat_children as $child ) {
// get the term object of each child term
$term = get_term( $child, \'category\' );
// output the list item with link and label
$cat_children_list .= \'<li><a href="\' . get_term_link( $term ) . \'">\' . $term->name . \'</a><?li>\';
}
// finish up the list
$cat_children_list .= \'</ul>\';
// echo the output
echo $cat_children_list;