一种方法是修改当前代码并在其中添加链接:
function category_name_shortcode() {
global $post;
$post_id = $post->ID;
$catName = "";
foreach((get_the_category($post_id)) as $category){
$catName .= \'<a href="\' . get_term_link($category) . \'">\' . $category->name . \'</a>, \';
}
return $catName;
}
add_shortcode( \'post_category\', \'category_name_shortcode\' );
但是有一种更简单的方法,因为WP已经有了一种获取带有链接的类别列表的方法(
get_the_category_list
):
function category_name_shortcode() {
return get_the_category_list( \', \' );
}
add_shortcode( \'post_category\', \'category_name_shortcode\' );