うさぎのイラスト

ネットアンサー55備忘録

web技術を書いていきます

カスタム投稿タイプfunctions.php設定

-2015年09月01日-
カスタム投稿設定 タクソノミーをつけない時
1//(ギャラリー)カスタム投稿設定
2add_action('init', 'add_gallery_post_type');
3function add_gallery_post_type() {
4    $params = array(
5            'labels' => array(
6                    'name' => 'gallery',
7                    'singular_name' => 'gallery',
8                    'add_new' => '新規追加',
9                    'add_new_item' => 'galleryを新規追加',
10                    'edit_item' => 'galleryを編集する',
11                    'new_item' => '新規gallery',
12                    'all_items' => 'gallery一覧',
13                    'view_item' => 'ページを確認',
14                    'search_items' => '検索する',
15                    'not_found' => 'galleryが見つかりませんでした。',
16                    'not_found_in_trash' => 'ゴミ箱内にgalleryが見つかりませんでした。'
17            ),
18            'public' => true,
19            'has_archive' => true,
20            'menu_position' => 20,
21            'supports' => array(
22                    'title',
23                    'editor',
24                    'author',
25                    'custom-fields',
26            ),
27            'taxonomies' => array('gallery_cat')
28    );
29    register_post_type('gallery', $params);
30     
31}
カスタム投稿設定 タクソノミーをつける時
1//(phpの)カスタム投稿設定
2add_action('init', 'add_ph_post_type');
3function add_ph_post_type() {
4    $params = array(
5            'labels' => array(
6                    'name' => 'php',
7                    'singular_name' => 'ph',
8                    'add_new' => '新規追加',
9                    'add_new_item' => 'phpを新規追加',
10                    'edit_item' => 'phpを編集する',
11                    'new_item' => '新規php',
12                    'all_items' => 'php一覧',
13                    'view_item' => 'ページを確認',
14                    'search_items' => '検索する',
15                    'not_found' => 'phpが見つかりませんでした。',
16                    'not_found_in_trash' => 'ゴミ箱内にphpが見つかりませんでした。'
17            ),
18            'public' => true,
19            'has_archive' => true,
20            'menu_position' => 20,
21            'supports' => array(
22                    'title',
23                    'editor',
24                    'author',
25                    'custom-fields',
26            ),
27            'taxonomies' => array('ph_cat')
28    );
29    register_post_type('ph', $params);
30     
31    /* カスタムタクソノミーを定義 */
32    register_taxonomy(
33        'ph_cat',
34        'ph',
35        array(
36        'label' => 'phカテゴリーから探す',
37        'hierarchical' => true,//カテゴリタイプ
38        'rewrite' => array('slug' => 'area')
39        )
40    );
41}
42 
43/* 管理画面一覧にカテゴリを表示 */
44function manage_ph_columns($columns) {
45    $columns['ph_cat'] = "phカテゴリー";
46    return $columns;
47}
48 
49function add_ph_column($column_name, $post_id){
50 
51    //場所で探す
52    if( $column_name == 'ph_cat' ) {
53        //カテゴリー名取得
54        if( 'ph_cat' == $column_name ) {
55            $ph_category = get_the_term_list($post_id, 'ph_cat', '', ', ', '' );
56        }
57 
58        //該当カテゴリーがない場合「なし」を表示
59        if ( isset($ph_category) && $ph_category ) {
60                 
61                echo $ph_category;
62            } else {
63                echo __('None');
64        }
65 
66    }
67}
68add_filter('manage_edit-ph_columns', 'manage_ph_columns');
69add_action('manage_posts_custom_column''add_ph_column', 10, 2);
参考サイト:かちびと.net