我有一個自定義文章類型「 ioni_codex」 我使用內置的WordPress的類別爲分類get_categories自定義柱式
我想列出了「ioni_codex」使用的所有類別。
我認爲這個代碼將這樣的伎倆:
$myargs = array (
'type' => 'ioni_codex'
);
$categories = get_categories($myargs);
但是相反,我看到的所有類別不是「ioni_codex」分配到的類別列表。
我在做什麼錯?
我有一個自定義文章類型「 ioni_codex」 我使用內置的WordPress的類別爲分類get_categories自定義柱式
我想列出了「ioni_codex」使用的所有類別。
我認爲這個代碼將這樣的伎倆:
$myargs = array (
'type' => 'ioni_codex'
);
$categories = get_categories($myargs);
但是相反,我看到的所有類別不是「ioni_codex」分配到的類別列表。
我在做什麼錯?
get_categories()
不接受post_type
作爲參數,請改爲使用taxonomy
,並按註冊時給出的名稱引用分類。這裏是一個鏈接,可以更詳細地解釋它的法典 - http://codex.wordpress.org/Function_Reference/get_categories。
我有一個妹妹項目答案: Bainternet♦已重新編寫get_terms()函數,以提供post_type
請參照他的解決方案here或只是複製和過去從下面:
/* get terms limited to post type
@ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
@ $args - (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
@ $post_type - (string|array) of post types to limit the terms to
@ $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
$args = array(
'post_type' => (array)$post_type,
'posts_per_page' => -1
);
$the_query = new WP_Query($args);
$terms = array();
while ($the_query->have_posts()){
$the_query->the_post();
$curent_terms = wp_get_object_terms($post->ID, $taxonomy);
foreach ($curent_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $c;
}
}
}
wp_reset_query();
//return array of term objects
if ($fields == "all")
return $terms;
//return array of term ID's
if ($fields == "ID"){
foreach ($terms as $t){
$re[] = $t->term_id;
}
return $re;
}
//return array of term names
if ($fields == "name"){
foreach ($terms as $t){
$re[] = $t->name;
}
return $re;
}
// get terms with get_terms arguments
if ($fields == "get_terms"){
$terms2 = get_terms($taxonomies, $args);
foreach ($terms as $t){
if (in_array($t,$terms2)){
$re[] = $t;
}
}
return $re;
}
}
我可以使用「類型」,它仍然沒有促成所需的結果。 我看着get_terms,它似乎並沒有考慮post_type什麼。 我需要的是列出'ioni_codex'出現的類別。相反,它列出我所有類別 – ioni 2012-07-27 00:48:06
自定義帖子類型沒有類別。 – 2012-07-27 07:36:23
我剛剛注意到,您提到您在自定義帖子類型(CPT)中使用了默認的WordPress類別。要獲得僅用於CPT的類別,您需要創建單獨的分類標準並將其註冊到CPT。這裏是Custon分類法典頁的鏈接:http://codex.wordpress.org/Taxonomies#Custom_Taxonomies – 2012-07-27 13:49:25