// カスタム投稿タイプ function create_my_post_types() { register_post_type( 'cult',//投稿タイプ名(識別子:半角英数字の小文字) array( 'label' => 'カルト事件簿', 'labels' => array( 'add_new' => '新規事件追加', 'edit_item' => '記事編集', 'view_item' => 'カルト事件簿表示', 'search_items' => 'カルト事件検索', 'not_found' => 'カルト事件の記事は見つかりませんでした。', 'not_found_in_trash' => 'ゴミ箱にカルト事件の記事はありません。', ), 'public' => true, 'description' => 'カスタム投稿タイプ「カルト事件簿」の説明文です。', 'hierarchical' => false, //固定ページのように親子関係true、投稿記事のように時系列に従ってコンテンツを管理false 'has_archive' => true, //trueにすると投稿した記事の一覧ページを作成することができる 'show_in_rest' => true, //新エディタ Gutenberg を有効化(REST API を有効化) 'supports' => array( //記事編集画面に表示する項目を配列で指定することができる 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions' ), 'menu_icon' => 'dashicons-format-aside', //追記メニューのアイコン 'exclude_from_search' => true, //検索対象。「true」で除外、「false」で含める 'menu_position' => 5, //「投稿」の下に追加 'taxonomies' => array('cult_cat', 'cult_tag') //使用するタクソノミー ) ); register_post_type( 'best10',//投稿タイプ名 array( 'label' => '今日のベストテン', 'labels' => array( 'add_new' => '新規ベスト10追加', 'edit_item' => 'ベスト10編集', 'view_item' => 'ベスト10表示', 'search_items' => 'ベスト10検索', 'not_found' => '今日のベスト10の記事は見つかりませんでした。', 'not_found_in_trash' => 'ゴミ箱に今日のベスト10の記事はありません。', ), 'public' => true, 'description' => 'カスタム投稿タイプ「今日のベスト10」の説明文です。', 'hierarchical' => false, 'has_archive' => true, 'show_in_rest' => true, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions' ), 'menu_icon' => 'dashicons-format-audio', //追記メニューのアイコン 'exclude_from_search' => true, //検索対象。「true」で除外、「false」で含める 'menu_position' => 6, //「投稿」の下の下に追加 'taxonomies' => array('best10_cat', 'best10_tag') //使用するタクソノミー ) ); //カスタムタクソノミー(カルトカテゴリー:カテゴリー形式)の登録 register_taxonomy( 'cult_cat', //カスタムタクソノミー名 'cult', //このタクソノミーが使われる投稿タイプ array( 'label' => 'カルトカテゴリー', //カスタムタクソノミーのラベル 'labels' => array( 'popular_items' => 'よく使うカルトカテゴリー', 'edit_item' =>'カルトカテゴリーを編集', 'add_new_item' => '新規カルトカテゴリーを追加', 'search_items' => 'カルトカテゴリーを検索' ), 'public' => true, // 管理画面及びサイト上に公開 'description' => 'カルトカテゴリーの説明文です。', //説明文 'hierarchical' => true, //カテゴリー形式 'show_in_rest' => true //Gutenberg で表示 ) ); register_taxonomy( 'best10_cat', //カスタムタクソノミー名 'best10', //このタクソノミーが使われる投稿タイプ array( 'label' => 'ベスト10カテゴリー', //カスタムタクソノミーのラベル 'labels' => array( 'popular_items' => 'よく使うベスト10カテゴリー', 'edit_item' =>'ベスト10カテゴリーを編集', 'add_new_item' => '新規ベスト10カテゴリーを追加', 'search_items' => 'ベスト10カテゴリーを検索' ), 'public' => true, // 管理画面及びサイト上に公開 'description' => 'ベスト10カテゴリーの説明文です。', //説明文 'hierarchical' => true, //カテゴリー形式 'show_in_rest' => true //Gutenberg で表示 ) ); //カスタムタクソノミー(カルトタグ:タグ形式)の登録 register_taxonomy( 'cult_tag', //カスタムタクソノミー名 'cult', //このタクソノミーが使われる投稿タイプ array( 'label' => 'カルトタグ', //カスタムタクソノミーのラベル 'labels' => array( 'popular_items' => 'よく使うカルトタグ', 'edit_item' =>'カルトタグを編集', 'add_new_item' => '新規カルトタグを追加', 'search_items' => 'カルトタグを検索' ), 'public' => true, // 管理画面及びサイト上に公開 'description' => 'カルトタグの説明文です。', //説明文 'hierarchical' => false, //タグ形式 'update_count_callback' => '_update_post_term_count', 'show_in_rest' => true //Gutenberg で表示 ) ); register_taxonomy( 'best10_tag', //カスタムタクソノミー名 'best10', //このタクソノミーが使われる投稿タイプ array( 'label' => 'ベスト10タグ', //カスタムタクソノミーのラベル 'labels' => array( 'popular_items' => 'よく使うベスト10タグ', 'edit_item' =>'ベスト10タグを編集', 'add_new_item' => '新規ベスト10タグを追加', 'search_items' => 'ベスト10タグを検索' ), 'public' => true, // 管理画面及びサイト上に公開 'description' => 'ベスト10タグの説明文です。', //説明文 'hierarchical' => false, //タグ形式 'update_count_callback' => '_update_post_term_count', 'show_in_rest' => true //Gutenberg で表示 ) ); } //init アクションフックで登録 add_action('init', 'create_my_post_types'); //カテゴリーアーカイブにカスタム投稿タイプ cult を含める(表示させる) function add_my_post_category_archive( $query ) { if (! is_admin() && $query->is_main_query() && $query->is_category()) { $query->set( 'post_type', array('post','cult')); } } add_action( 'pre_get_posts', 'add_my_post_category_archive' ); //タグアーカイブにカスタム投稿タイプ cult を含める(表示させる) function add_my_post_tag_archive( $query ) { if (! is_admin() && $query->is_main_query() && $query->is_tag()) { $query->set( 'post_type', array('post','cult')); } } add_action( 'pre_get_posts', 'add_my_post_tag_archive' ); add_filter('title_parts_custom', function ($title){ //カルト事件簿のカスタム投稿タイプ名 if (is_singular('cult')) { $title['title'] = 'カルト事件簿('.$title['title'].')'; } return $title; }); add_filter('title_parts_custom', function ($title){ //今日のベストテンのカスタム投稿タイプ名 if (is_singular('best10')) { $title['title'] = '今日のベストテン('.$title['title'].')'; } return $title; }); //カスタム投稿タイプ一覧に「日付」を追加項目。 function add_custom_column($columns) { $columns['cult_date'] = 'カスタム日付'; $columns['cult_cat'] = 'タクソノミー'; return $columns; } add_filter( 'manage_cult_posts_columns', 'add_custom_column' ); // カスタムフィールドの内容を表示 function add_column($column_name, $post_id) { if( $column_name == 'cult_date' ) { $stitle = get_post_meta($post_id, 'cult_date', true); } if ( isset($stitle) && $stitle ) { echo attribute_escape($stitle); } else { echo __(''); } $terms = get_the_terms($post_id, $column_name); if ( $terms && ! is_wp_error( $terms ) ){ $cult_cat_links = array(); foreach ( $terms as $term ) { $cult_cat_links[] = $term->name; } echo join( ", ", $cult_cat_links ); } } add_action( 'manage_cult_posts_custom_column', 'add_column', 10, 2 ); function add_custom_column_best10($columns) { $columns['cult_date'] = 'カスタム日付'; $columns['best10_cat'] = 'タクソノミー'; return $columns; } add_filter( 'manage_best10_posts_columns', 'add_custom_column_best10' ); // カスタムフィールドの内容を表示 function add_column_best10($column_name, $post_id) { if( $column_name == 'cult_date' ) { $stitle = get_post_meta($post_id, 'cult_date', true); } if ( isset($stitle) && $stitle ) { echo attribute_escape($stitle); } else { echo __(''); } $terms = get_the_terms($post_id, $column_name); if ( $terms && ! is_wp_error( $terms ) ){ $best10_cat_links = array(); foreach ( $terms as $term ) { $best10_cat_links[] = $term->name; } echo join( ", ", $best10_cat_links ); } } add_action( 'manage_best10_posts_custom_column', 'add_column_best10', 10, 2 );