有時(shí)候,我們需要獲取文章所在的自定義分類法的分類項(xiàng)目名稱,以表示文章所在的分類,如下圖。

這種需求應(yīng)該是相當(dāng)小眾的,所以WordPress肯定不會(huì)提供這樣的函數(shù),怎么辦?涼拌肯定不行,那就只有我們自己動(dòng)手了,以下是具體的代碼。
function custom_taxonomies_terms_links(){
//根據(jù)當(dāng)前文章ID獲取文章信息
$post = get_post( $post->ID );
//獲取當(dāng)前文章的文章類型
$post_type = $post->post_type;
//獲取文章所在的自定義分類法
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
$term_list = wp_get_post_terms($post->ID, $taxonomy_slug, array("fields" => "all"));
echo $term_list[0]->name; //顯示文章所處的分類中的第一個(gè)
}
return implode('', $out );
}
以上代碼對(duì)于文章在自定義分類法和默認(rèn)分類目錄的時(shí)候都是可用的。



請(qǐng)問這段代碼放在functions.php 里面嗎,前臺(tái)如何調(diào)用,正在遇到這方面的問題 ??