web-dev-qa-db-ja.com

カスタム投稿タイプとカテゴリ

私の人生のために私は私のカスタム投稿タイプでカテゴリーが存在するようになるようには思えません。私のテーマのfunctions.phpファイルの最後に次のような単純なコードを追加しましたが、カスタム投稿の管理者側からカテゴリが表示されません。

register_post_type("customy", array(
    'label' => 'Customy',
    'description' => 'Custom stuff for this site.',
    'public' => true,
    'hierarchical' => true,
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'revisions'),
    'taxonomies' => array('category')
));
register_taxonomy_for_object_type('category', 'customy');
1
Sampson

register_post_type()はすぐに新しいpost_typeを追加しますが、post_typeに関連するカテゴリ分類のためにロジックを関数に束縛してinitアクションに追加する必要があるかのようです。実用的な例は次のとおりです。

function add_articles_post_type() {
  register_post_type("article", array(
    'label' => 'Article',
    'public' => true,
    'hierarchical' => true,
    'supports' => array('title','editor','author','thumbnail','revisions')
  ));
  register_taxonomy_for_object_type('category', 'article');
}
add_action('init', 'add_articles_post_type');
5
Sampson