0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

1つのカスタム投稿に複数のタクソノミーを設定する

Last updated at Posted at 2022-01-14

タクソノミ複数.png
●スラッグ
「レッスン」 → lesson
「ギター」 → lesson_guitar
「ベース」 → lesson_bass
「ドラム」 → lesson_drum

functions.php
//カスタム投稿タイプ追加
register_post_type(
'lesson', //カスタム投稿のスラッグ
array(
'label' => 'レッスン検索', //管理画面に表示される名前
'hierarchical' => true,
'has_archive' => true,
'public' => true,
'menu_position' => 5,
'supports' => array('title','editor'),
'exclude_from_search' => false,
));
//カスタムタクソノミー追加
add_action( 'init', 'add_custom_taxonomy_event', 0 );
function add_custom_taxonomy_event() {
register_taxonomy(
'lesson_guitar', //カスタムタクソノミーのスラッグ
'lesson', //上のカスタム投稿タイプ追加で指定したスラッグ
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => 'ギター', //管理画面に表示される名前
'public' => true,
'show_ui' => true,
));
}
add_action( 'init', 'add_custom_taxonomy_event02', 0 );
function add_custom_taxonomy_event02() {
register_taxonomy(
'lesson_bass', //カスタムタクソノミーのスラッグ
'lesson', //上のカスタム投稿タイプ追加で指定したスラッグ
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => 'ベース', //管理画面に表示される名前
'public' => true,
'show_ui' => true,
));
}
add_action( 'init', 'add_custom_taxonomy_event03', 0 );
function add_custom_taxonomy_event03() {
register_taxonomy(
'lesson_drum', //カスタムタクソノミーのスラッグ
'lesson', //上のカスタム投稿タイプ追加で指定したスラッグ
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => 'ドラム', //管理画面に表示される名前
'public' => true,
'show_ui' => true,
));
}

●管理画面の表示
複数タクソノミ表示.png
何やらスマートな書き方じゃない気がしますが…
とりあえず機能はしてます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?