よく使う!functions.phpに書くカスタム投稿タイプの基本形【WordPress】

Author:

最近頻度多くて、あちこちからコードをコピペするのめんどいので、ここに個人的メモ。

// カスタム投稿タイプ「お知らせ」を登録
register_post_type('news',
  array(
    'labels' => array(
      'name' => 'お知らせ',
      'singular_name' => 'お知らせ',
      'add_new' => 'お知らせの新規追加',
      'add_new_item' => 'お知らせの新規追加',
      'edit_item' => 'お知らせの編集',
      'view_item' => 'お知らせを表示',
      'search_items' => 'お知らせを検索',
      'not_found' => 'お知らせは見つかりませんでした。',
      'not_found_in_trash' => 'ゴミ箱にお知らせはありませんでした。',
    ),
    'public' => true,
    'description' => 'カスタム投稿タイプ「お知らせ」',
    'hierarchical' => false,
    'has_archive' => true,
    'show_in_rest' => true,
    'supports' => array(
      'title',
      'editor',
      'thumbnail',
      'excerpt',
      'custom-fields',
      'revisions'
    ),
    'menu_position' => 5,
    'rewrite' => array('slug' => 'news'),
  )
);
// カスタム投稿タイプ「お知らせ」のタクソノミーを追加
register_taxonomy('news_cat', 'news', array(
  'label' => 'カテゴリー',
  'hierarchical' => true,
  'public' => true,
  'show_in_rest' => true,
  'rewrite' => array('slug' => 'news_cat'),
));