WordPressでカスタム投稿タイプを複数つくる

WordPressでカスタム投稿タイプを追加してみる

WordPressでカスタム投稿タイプを作成する方法をネットで調べてみると、1つの投稿タイプを追加する方法はよく見つかるのであるが、複数を追加する場合の例がなかなか見つからない。

そこで、実際にカスタム投稿タイプを2つ追加してみたので記録に残しておく。

function.phpに以下のコードを追記する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//カスタム投稿タイプの追加
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