WordPressでカスタム投稿タイプを追加してみる
WordPressでカスタム投稿タイプを作成する方法をネットで調べてみると、1つの投稿タイプを追加する方法はよく見つかるのであるが、複数を追加する場合の例がなかなか見つからない。
そこで、実際にカスタム投稿タイプを2つ追加してみたので記録に残しておく。
function.phpに以下のコードを追記する。
//カスタム投稿タイプの追加
add_action( 'init', 'create_post_type' );
function create_post_type() {
//カスタム投稿タイプ1(ここでは、"custom1"としておく)
register_post_type(
'custom1', array(
'label' => 'カスタム1', //ラベル名をつける
'hierarchical' => false, //falseの場合、階層構造なし
'public' => true, //通常はtrue
'has_archive' => true, //trueでindexページを生成
'supports' => array(
'title', //タイトルの表示を有効に
'editor' //本文の表示を有効に
)
)
);
//カスタム投稿タイプ2(ここでは、"custom2"としておく)
register_post_type(
'custom2', array(
'label' => 'カスタム2', //ラベル名をつける
'hierarchical' => false, //trueの場合、階層構造あり
'public' => true, //通常はtrue
'has_archive' => true, //trueでindexページを生成
'supports' => array(
'title', //タイトルの表示を有効に
'editor', //本文の表示を有効に
'thumbnail' //サムネイルを有効にする
)
)
);
}
photo by:
bobbigmac