カスタム投稿タイプとカスタム分類とは・・
カスタム投稿タイプ(Custom Post Types)・・「投稿」のようなメニュー項目を追加する事
カスタム分類(Custom Taxonomies)・・「カテゴリ」「タグ」のような分類の事
例えば、“販売店”(store) という「カスタム投稿タイプ」を作成して
「カスタム分類」で “エリア”(area) “取扱商品”(product) を追加してみましょう
今回は、プラグインを使わずにfunctions.php(テーマファイル)にコードを追加します。
※プラグインの場合は「Custom Post Type UI」がおすすめです。
functions.php
/* カスタム投稿タイプの追加 */
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'store', /* 投稿タイプ名 */
array(
'labels' => array(
'name' => __( '販売店' ), /* 管理画面の表示名 */
'singular_name' => __( '販売店' )
),
'public' => true,
'menu_position' => 5,
'has_archive' => true, /* アーカイブ生成 */
/* 投稿画面の表示項目 */
'supports' => array('title','editor','thumbnail','custom-fields','excerpt','author','page-attributes')
)
);
/* カスタム分類を作成 - エリアの追加 */
register_taxonomy(
'area', /* タクソノミー名 */
'store', /* 投稿タイプ名' */
array(
'hierarchical' => true, /* 階層化あり */
'update_count_callback' => '_update_post_term_count',
'label' => 'エリア',
'singular_label' => 'エリア',
'public' => true,
'show_ui' => true, /* 項目の表示 */
'query_var' => true
)
);
/*カスタム分類を作成 - 取扱商品の追加 */
register_taxonomy(
'product', /* タクソノミー名 */
'store',
array(
'hierarchical' => false, /* 階層化なし */
'update_count_callback' => '_update_post_term_count',
'label' => '取扱商品',
'public' => true,
'show_ui' => true,
'query_var' => true
)
);
}
カスタム投稿タイプでコメントを受け付ける場合・・
上記コード内のパラメータ 引数 ’supports‘ に以下のように追加。
(コメント→ ’comments‘ 、トラックバック→ ’trackbacks‘ )
参考:関数リファレンスregister_post_type
カスタム投稿タイプ作成後の画面
下の赤枠部分が新規に作成されました。※「販売店」という投稿に「エリア」「取扱商品」の分類

カスタム分類「エリア」の画面・・・”カテゴリー”のように階層化あり

カスタム分類「取扱商品」の画面・・・”タグ”のように階層化なし

カスタム投稿タイプの投稿一覧にタクソノミーを表示する
functions.php(テーマファイル)に以下コードを追加
/* カスタム投稿タイプの投稿一覧にタクソノミーを表示 */
function manage_posts_columns($columns) {
$columns['area'] = 'エリア';
$columns['product'] = '取扱商品';
return $columns;
}
function add_column($column_name, $post_id) {
/* エリアのターム一覧 */
if( $column_name == 'area' ) {
echo get_the_term_list($id, 'area');
}
/* 取扱商品のターム一覧 */
if( $column_name == 'product' ) {
echo get_the_term_list($id, 'product');
}
}
add_filter( 'manage_store_posts_columns', 'manage_posts_columns' ); /* 一覧 */
add_action( 'manage_posts_custom_column', 'add_column', 10, 2 ); /* 中身 */
ダッシュボードに投稿数を表示

カスタム投稿タイプの投稿数はfunctions.php(テーマファイル)に以下コードを追加
/* カスタム投稿タイプの投稿数をダッシュボードに表示 */
function custom_post_dashboard() {
$custom_post_type = 'store'; // カスタム投稿のラベル
global $wp_post_types;
$num_post_type = wp_count_posts( $custom_post_type );
$num = number_format_i18n($num_post_type->publish);
$text = _n( $wp_post_types[$custom_post_type]->labels->singular_name, $wp_post_types[$custom_post_type]->labels->name, $num_post_type->publish );
$capability = $wp_post_types[$custom_post_type]->cap->edit_posts;
if (current_user_can($capability)) {
$num = "<a href='edit.php?post_type=" . $custom_post_type . "'>$num</a>";
$text = "<a href='edit.php?post_type=" . $custom_post_type . "'>$text</a>";
}
echo '<tr>';
echo '<td class="first b b_' . $custom_post_type . '">' . $num . '</td>';
echo '<td class="t ' . $custom_post_type . '">' . $text . '</td>';
echo '</tr>';
}
add_action('right_now_content_table_end', 'custom_post_dashboard');
カスタム分類のダッシュボードの表示にはプラグイン「PS Taxonomy Expander」を使用してみました。
以下のようにタクソノミーについていろいろな設定ができて便利です。


参考サイト
- WordPress Codex 日本語版 カスタム投稿タイプ
- WordPress Codex 日本語版 関数リファレンス/register post type
- WordPress Codex 日本語版 関数リファレンス/register taxonomy
- カスタム投稿タイプの投稿数をダッシュボードに表示する
- WordPress管理画面の投稿記事一覧をカスタマイズする
次回はカスタム投稿タイプとカスタムタクソノミーの表示についての予定です・・。