プラグインを使わずにカスタム投稿やカスタムタクソノミーを追加する方法

プラグインを使わずにカスタム投稿やカスタムタクソノミーを追加する方法

プラグインを使わずにカスタム投稿やカスタムタクソノミーを追加する方法

カスタム投稿やカスタムタクソノミーをプラグインなしで追加する方法をご紹介いたします。

Custom Post Type UIToolset Types等のプラグインを使った方が楽にカスタム投稿やカスタムタクソノミーを追加することができますが、シンプルなものであればプラグインを使わなくても割と簡単に追加することが可能です。

スポンサードリンク

プラグインを使わずにカスタム投稿を追加する方法

カスタム投稿を追加するには、register_post_type()関数を使用します。functions.phpに以下のようなコードを追加することで、カスタム投稿を定義します。

function add_custom_post_type() {
  $args = array(
    'label' => 'カスタム投稿のラベル', //カスタム投稿のラベル
    'hierarchical' => false, //階層構造の有無
    'public' => true, //投稿の公開
    'menu_position' => 5, //メニューの位置
    'has_archive' => true, //アーカイブの生成
    'menu_icon' => 'Dashiconsの名前', //メニューのアイコン
    'show_in_rest' => true, //Gutenberg(ブロックエディタ)に対応
    'rewrite' => array('with_front' => false), //URLから共通設定のパーマリンクを除外
    'supports' => array(
      'title', //タイトルを有効化
      'editor', //本文を有効化
      'thumbnail' //アイキャッチ画像の有効化
    )
  );
  register_post_type( 'カスタム投稿名', $args );
}
add_action( 'init', 'add_custom_post_type' );

カスタム投稿名やラベルは任意のものを指定してください。また、menu_iconの値は、Dashiconsの名前(dashicons-clock等)を指定します。

上記で十分事足りるかとは思いますが、より細かく指定したい場合は以下のようにします。

add_action( 'init', 'codex_book_init' );
/**
* カスタム投稿タイプ book を登録する。
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_book_init() {
  $labels = array(
    'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
    'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
    'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
    'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
    'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
    'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
    'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
    'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
    'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
  );
 
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'show_in_rest' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );
 
  register_post_type( 'book', $args );
}

register_post_type()の詳細は、関数リファレンスをご参照ください。

なお、カスタム投稿でカテゴリーとタグを使用したい場合は、以下のようにregister_taxonomy_for_object_typeを追記してあげます。

function add_custom_post_type() {
  $args = array(
    'label' => 'カスタム投稿のラベル', //カスタム投稿のラベル
    'hierarchical' => false, //階層構造の有無
    'public' => true, //投稿の公開
    'menu_position' => 5, //メニューの位置
    'has_archive' => true, //アーカイブの生成
    'menu_icon' => 'Dashiconsの名前', //メニューのアイコン
    'show_in_rest' => true, //Gutenberg(ブロックエディタ)に対応
    'rewrite' => array('with_front' => false), //URLから共通設定のパーマリンクを除外
    'supports' => array(
      'title', //タイトルを有効化
      'editor', //本文を有効化
      'thumbnail' //アイキャッチ画像の有効化
    )
  );
  register_post_type('カスタム投稿名', $args);
  register_taxonomy_for_object_type('category', 'カスタム投稿名');
  register_taxonomy_for_object_type('post_tag', 'カスタム投稿名');
}
add_action( 'init', 'add_custom_post_type' );

プラグインを使わずにカスタムタクソノミーを追加する方法

カスタムタクソノミーを追加するには、register_taxonomy()関数を使用します。functions.phpに以下のようなコードを追加することで、カスタムタクソノミーを定義します。

function add_custom_tax() {
  $args = array(
    'hierarchical' => true, //階層構造の有無。falseでタグ形式
    'label' => 'カスタムタクソノミーのラベル', //タクソノミーのラベル
    'show_ui' => true, //タームを管理するためにデフォルトのUIを用意
    'show_admin_column' => true, //管理画面の投稿一覧に表示
    'query_var' => true, //アーカイブを作成
    'rewrite' => array('with_front' => false), //URLから共通設定のパーマリンクを除外
    'show_in_rest' => true //Gutenberg(ブロックエディタ)に対応
  );
  register_taxonomy( 'カスタムタクソノミー名', 'カスタム投稿名', $args );
}
add_action( 'init', 'add_custom_tax' );

カスタムタクソノミー名やカスタム投稿名、ラベル等は任意のものに変更してください。

より細かく指定したい場合は、以下のようにします。

// init アクションにフックして、そのタイミングで create_book_taxonomies を呼び出す
add_action( 'init', 'create_book_taxonomies', 0 );
 
// "book" カスタム投稿タイプに対して genres と writers という2つのカスタム分類を作成する
function create_book_taxonomies() {
  // (カテゴリーのような)階層化したカスタム分類を新たに追加
  $labels = array(
    'name' => _x( 'Genres', 'taxonomy general name' ),
    'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
    'search_items' => __( 'Search Genres' ),
    'all_items' => __( 'All Genres' ),
    'parent_item' => __( 'Parent Genre' ),
    'parent_item_colon' => __( 'Parent Genre:' ),
    'edit_item' => __( 'Edit Genre' ),
    'update_item' => __( 'Update Genre' ),
    'add_new_item' => __( 'Add New Genre' ),
    'new_item_name' => __( 'New Genre Name' ),
    'menu_name' => __( 'Genre' ),
  );
 
  $args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'show_in_rest' => true,
    'rewrite' => array( 'slug' => 'genre', 'with_front' => false ),
  );
 
  register_taxonomy( 'genre', array( 'book' ), $args );
 
  // (タグのような)階層のないカスタム分類を新たに追加
  $labels = array(
    'name' => _x( 'Writers', 'taxonomy general name' ),
    'singular_name' => _x( 'Writer', 'taxonomy singular name' ),
    'search_items' => __( 'Search Writers' ),
    'popular_items' => __( 'Popular Writers' ),
    'all_items' => __( 'All Writers' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Writer' ),
    'update_item' => __( 'Update Writer' ),
    'add_new_item' => __( 'Add New Writer' ),
    'new_item_name' => __( 'New Writer Name' ),
    'separate_items_with_commas' => __( 'Separate writers with commas' ),
    'add_or_remove_items' => __( 'Add or remove writers' ),
    'choose_from_most_used' => __( 'Choose from the most used writers' ),
    'not_found' => __( 'No writers found.' ),
    'menu_name' => __( 'Writers' ),
  );
 
  $args = array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'show_in_rest' => true,
    'rewrite' => array( 'slug' => 'writer', 'with_front' => false ),
  );
 
  register_taxonomy( 'writer', 'book', $args );
}

register_taxonomy()関数の詳細については、関数リファレンスをご参照ください。

あとがき

カスタム投稿もカスタムタクソノミーも細かく定義する必要がなければ、追加するのはそこまで難しくはないですね。

ちなみに、以下のように一纏めにして指定してもOKです。

function add_custom_post_type() {
  $args = array(
    'label' => 'カスタム投稿のラベル', //カスタム投稿のラベル
    'hierarchical' => false, //階層構造の有無
    'public' => true, //投稿の公開
    'menu_position' => 5, //メニューの位置
    'has_archive' => true, //アーカイブの生成
    'menu_icon' => 'Dashiconsの名前', //メニューのアイコン
    'show_in_rest' => true, //Gutenberg(ブロックエディタ)に対応
    'rewrite' => array('with_front' => false), //URLから共通設定のパーマリンクを除外
    'supports' => array(
      'title', //タイトルを有効化
      'editor', //本文を有効化
      'thumbnail' //アイキャッチ画像の有効化
    )
  );
  register_post_type( 'カスタム投稿名', $args );
 
  $tax_args = array(
    'hierarchical' => true, //階層構造の有無。falseでタグ形式
    'label' => 'カスタムタクソノミーのラベル', //タクソノミーのラベル
    'show_ui' => true, //タームを管理するためにデフォルトのUIを用意
    'show_admin_column' => true, //管理画面の投稿一覧に表示
    'query_var' => true, //アーカイブを作成
    'rewrite' => array('with_front' => false), //URLから共通設定のパーマリンクを除外
    'show_in_rest' => true //Gutenberg(ブロックエディタ)に対応
  );
  register_taxonomy( 'カスタムタクソノミー名', 'カスタム投稿名', $tax_args );
}
add_action( 'init', 'add_custom_post_type' );

プラグインを使わずにカスタム投稿やカスタムタクソノミーを追加したい場合は、ぜひ参考にしていただければと思います。

この記事が気に入ったら
いいね!してね♪

Twitter で
スポンサードリンク

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です