カスタム投稿タイプの登録方法
2021年3月4日
functions.phpに以下のコードを追加する。
この場合は”art”というカスタム投稿入力項目がWordpress編集画面に累加される。
// カスタム投稿タイプの追加
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'art', [ // 投稿タイプ名の定義
'labels' => [
'name' => 'Art作品', // 管理画面上で表示する投稿タイプ名
'singular_name' => 'art', // カスタム投稿の識別名
],
'public' => true, // 投稿タイプをpublicにするか
'has_archive' => false, // アーカイブ機能ON/OFF
'menu_position' => 5, // 管理画面上での配置場所
'show_in_rest' => false, // 5系から出てきた新エディタ「Gutenberg」を有効にする
]);
}
出力にはsingle-art.phpを用意するかsingle.phpで条件分岐で読み込む場合は
以下のように、is_singularを使う。
<?php
if(in_category('news')) {
include(TEMPLATEPATH.'/category-news.php');
}
elseif(in_category(array('mural','design'))) {
include(TEMPLATEPATH.'/category-art.php');
}
elseif(is_singular('art')) {
include(TEMPLATEPATH.'/single-art.php');
}
else {
include(TEMPLATEPATH.'/single-default.php');
}
?>