自动获取文章所属分类,如果为列表仅显示分类名,如果为详情页则显示分类名+链接
if ( has_category() ) {
$categories = get_the_category();
echo '<span class="the-categories">';
foreach( $categories as $category ){
if ( is_singular() ) {
$the_cat_link = get_category_link( $category->term_id );
echo '<a class="cat-item" href="' .$the_cat_link. '">' .$category->cat_name. '</a>';
} else {
echo '<span class="cat-item">' .$category->cat_name. '</span>';
}
}
echo '</span>';
}