ご指定された単一記事を表示しています。
2013年7月20日 タグ:tips WordPressカスタム投稿にカテゴリー・タグ機能を追加する
function.phpに以下のコードを記します。
*すでにカスタム投稿機能を追加済みの場合は、13行目以降を追加してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
//カスタム投稿の追加と、カテゴリー、タグ機能の追加 function my_custom_init() { //カスタム投稿「example」の設定 register_post_type( 'example', array( 'label' => 'example の投稿', 'public' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' ), 'menu_position' => 5, 'has_archive' => true )); //カテゴリタイプの設定(カスタムタクソノミーの設定) register_taxonomy( 'examplecat', //カテゴリー名(任意) 'example', //カスタム投稿名 array( 'hierarchical' => true, //カテゴリータイプの指定 'update_count_callback' => '_update_post_term_count', //ダッシュボードに表示させる名前 'label' => 'exampleのカテゴリー', 'public' => true, 'show_ui' => true ) ); //タグタイプの設定(カスタムタクソノミーの設定) register_taxonomy( 'exampletag', //タグ名(任意) 'example', //カスタム投稿名 array( 'hierarchical' => false, //タグタイプの指定(階層をもたない) 'update_count_callback' => '_update_post_term_count', //ダッシュボードに表示させる名前 'label' => 'exampleのタグ', 'public' => true, 'show_ui' => true ) ); } add_action( 'init', 'my_custom_init' ); |
- 注意
- カテゴリー名・タグ名に「–(ハイフン)」を用いると、カスタム投稿関連を表示するときに使われるテンプレートファイル(taxonomy-$taxonomy-$term.phpや、taxonomy-$taxonomy.php)で意図しない結果になる場合があります。
- 補足
-
- カスタム投稿を表示させるテンプレートファイル
- archive.php・archive-$taxonomy.phpなど
- 指定URL
- http://ドメイン名/カスタム投稿名
- カスタム投稿のカテゴリー・タグを表示させるテンプレートファイル
- taxonomy.php・taxonomy-$taxonomy.phpなど