カスタム投稿の作るコードとカスタム投稿でカテゴリとタグを使うためのコード
コピペだけ!カスタム投稿の作るコード
functions.php に、以下のコードを記述することで、カスタム投稿タイプが作れる。
追加する投稿タイプを仮に「news」、投稿の名前を「ニュース」とする。
変数はregister_post_typeを参考に。
add_action('init', 'my_custom_post_type');
function my_custom_post_type() {
$labels = array(
'name' => _x('ニュース', 'post type general name'),
'singular_name' => _x('ニュース', 'post type singular name'),
'add_new' => _x('新しいニュースを追加', 'book'),
'add_new_item' => __('新しいニュースを追加'),
'edit_item' => __('ニュースを編集'),
'new_item' => __('新しいニュース'),
'view_item' => __('ニュースを編集'),
'search_items' => __('ニュースを探す'),
'not_found' => __('ニュースはありません'),
'not_found_in_trash' => __('ゴミ箱にニュースはありません'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor')
);
register_post_type('news',$args);
}
コピペだけ!カスタム投稿でカテゴリとタグを使うためのコード
カテゴリを記事投稿画面に設置したい場合は、register_taxonomyのhierarchicalをtrueにする。
タグを記事投稿画面に設置場合は、register_taxonomyのhierarchicalをfalseにする。
もちろんこの2つは併用可能。
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'news', /* post-type */
array(
'labels' => array(
'name' => __( 'ニュース' ),
'singular_name' => __( 'ニュース' )
),
'public' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' ),
'menu_position' =>5,
'has_archive' => true
)
);
//カスタムタクソノミー、カテゴリタイプ
register_taxonomy(
'news-cat',
'news',
array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => 'ニュースのカテゴリー',
'singular_label' => 'ニュースのカテゴリー',
'public' => true,
'show_ui' => true
)
);
//カスタムタクソノミー、タグタイプ
register_taxonomy(
'news-tag',
'news',
array(
'hierarchical' => false,
'update_count_callback' => '_update_post_term_count',
'label' => 'ニュースのタグ',
'singular_label' => 'ニュースのタグ',
'public' => true,
'show_ui' => true
)
);
}