2012-07-26 40 views
2

我有一个自定义文章类型“ ioni_codex” 我使用内置的WordPress的类别为分类get_categories自定义柱式

我想列出了“ioni_codex”使用的所有类别。

我认为这个代码将这样的伎俩:

$myargs = array (
    'type' => 'ioni_codex' 
); 
$categories = get_categories($myargs); 

但是相反,我看到的所有类别不是“ioni_codex”分配到的类别列表。

我在做什么错?

回答

3

get_categories()不接受post_type作为参数,请改为使用taxonomy,并按注册时给出的名称引用分类。这里是一个链接,可以更详细地解释它的法典 - http://codex.wordpress.org/Function_Reference/get_categories

+0

我可以使用“类型”,它仍然没有促成所需的结果。 我看着get_terms,它似乎并没有考虑post_type什么。 我需要的是列出'ioni_codex'出现的类别。相反,它列出我所有类别 – ioni 2012-07-27 00:48:06

+0

自定义帖子类型没有类别。 – 2012-07-27 07:36:23

+0

我刚刚注意到,您提到您在自定义帖子类型(CPT)中使用了默认的WordPress类别。要获得仅用于CPT的类别,您需要创建单独的分类标准并将其注册到CPT。这里是Custon分类法典页的链接:http://codex.wordpress.org/Taxonomies#Custom_Taxonomies – 2012-07-27 13:49:25

0

我有一个妹妹项目答案: 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; 
    } 
}