Last active
August 29, 2015 13:56
-
-
Save YoshinoriKobayashi/9145496 to your computer and use it in GitHub Desktop.
WordPress functions.php register_post_type,register_taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// カスタム投稿の設定 | |
// 投稿タイプの codex | |
// http://wpdocs.sourceforge.jp/%E6%8A%95%E7%A8%BF%E3%82%BF%E3%82%A4%E3%83%97 | |
// register_post_type codex | |
// http://wpdocs.sourceforge.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/register_post_type | |
// ******************************************************* | |
// 現場で使われているWordPressデザインメソッドより P120、P122 | |
// | |
// カスタム投稿タイプの投稿一覧ページのURLは、「WordPressのサイトURL/カスタム投稿タイプ名/」になる。 | |
// | |
function create_post_type_staff(){ | |
$labels = array( | |
'name' => 'スタッフ', | |
'all_items' => 'スタッフの一覧', | |
); | |
$args = array( | |
'labels' => $labels, | |
'supports' => array('title','editor','excerpt','thumbnail'), | |
'public' => true, // 公開するかどうか true:公開 | |
'show_ui' => true, // メニューに表示するかどうか | |
'menu_position' => 5, // メニューの表示位置 | |
'has_archive' => true, // アーカイブページの作成 | |
'menu_icon' => get_bloginfo('templete_url').'/assets/img/icon-staff.png', | |
); | |
// 最初のパラメタ staff が投稿タイプの名前になる。 | |
register_post_type('staff',$args); | |
} | |
add_action('init','create_post_type_staff',0); | |
// カスタム分類(カスタムタクソノミー) | |
function create_custom_taxonomy_staff(){ | |
register_taxonomy( | |
'staff-cat', // カスタム分類名 | |
'staff', // カスタム分類を使用する投稿タイプ名 | |
array( | |
'hierarchical' => true, | |
'label' => 'スタッフカテゴリー', | |
'singular_label' => 'スタッフカテゴリー', | |
'public' => true, | |
'show_ui' => true, | |
) | |
); | |
} | |
add_action('init','create_custom_taxonomy_staff',0); | |
// カスタム分類のタームの取得 | |
// ループ内で使用する。 | |
echo get_the_term_list($post->ID,'staff-cat'); | |
// カスタム分類のリストを表示 | |
wp_list_categories('title_li=&taxonomy=staff-cat&orderby=order'); | |
// ******************************************************* | |
// WordPressの教科書2 | |
// P113 | |
// カスタム投稿タイプ・カスタム分類 | |
// add_action 中身はadd_filterと同じ? | |
// add_filterと異なり、WordPressの関数の挙動に変更を行わない。 | |
add_action('init','register_post_type_and_taxonomy'); | |
function register_post_type_and_taxonomy(){ | |
register_post_type( | |
'branch', | |
array( | |
'labels' => array( | |
'name' => '営業所情報', | |
'add_new_item' => '営業所を追加', | |
'edit_item' => '営業所の編集', | |
), | |
'public' => true, // 一般的な投稿タイプとして通常利用する場合は、true | |
'hierarchical' => true, // 階層の有無を指定 | |
'supports' => array( | |
'title', | |
'editor', | |
'excerpt', | |
'custom-fields', | |
'thumnail', | |
'page-attributes', | |
), | |
) | |
); | |
// カスタム投稿タイプ「tour」 | |
// hierarchical 省略しているので、非階層型の投稿タイプ | |
// supports page-attributes(ページ属性)を指定していない。非階層型のカスタム投稿タイプになります。 | |
register_post_type( | |
'tour', | |
array( | |
'labels' => array( | |
'name' => 'ツアー情報', | |
'add_new_item' => '新規ツアーを追加', | |
'edit_item' =>'ツアーの編集', | |
), | |
'public' => true, | |
'supports' => array( | |
'title', | |
'editor', | |
'excerpt', | |
'custom-fields', | |
'thumnail', | |
), | |
) | |
); | |
// カスタム分類の定義 | |
register_taxonomy( | |
'area', // 分類名 | |
'tour', // 紐づける投稿タイプ名 | |
array( | |
'labels' => array( | |
'name' => '地域', | |
'add_new_item' => '地域を追加', | |
'edit_item' => '地域の編集', | |
), | |
'hierarchical' => true, | |
'show_admin_column' => true, // 管理画面で分類を表示させる場合は、true | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment