3

カスタム投稿タイプ「ioni_codex」があり、分類法として組み込みのWordpressカテゴリを使用しています

'ioni_codex'で使用されているすべてのカテゴリを一覧表示したいと思います。

私はこのコードがトリックを行うと思います:

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

ただし、代わりに、「ioni_codex」によって割り当てられたカテゴリではなく、すべてのカテゴリのリストが表示されます。

私は何を間違っていますか?

4

3 に答える 3

6

get_categories()post_typeは引数として受け入れません。taxonomy代わりに使用し、登録時に指定した名前で分類法を参照してください。これは、それをより詳細に説明できるコーデックスへのリンクです -http://codex.wordpress.org/Function_Reference/get_categories

于 2012-07-26T21:35:41.407 に答える
0

私は姉妹プロジェクトで答えを持っています:Bainternet♦はpost_typeを提供するためにget_terms()関数を書き直しました

ここで彼の解決策を参照するか、以下からコピーして貼り付けてください。

/* 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;
    }
}
于 2012-07-27T14:15:53.723 に答える
0

タイプの代わりにpost_typeを設定する必要があります。デフォルトでは、get_categoriesは空のカテゴリを非表示にしようとします。すべてを表示する場合は、hide_emptyプロパティをfalseに設定します。

 get_categories(array(
    'post_type' => 'ioni_codex',
    'hide_empty' => false,
   ) );

于 2021-09-24T04:51:10.950 に答える