WordPress でカスタム投稿タイプを簡単に拡張できるようにするコード

カスタム投稿タイプを拡張するためのプラグインがあります。しかしプラグイン頼りだとプラグインがなくなった時に困りますし、テーマで直接有効化したいという場合もあると思いますので、今回は直接拡張を簡単にする方法を紹介します。

カスタム投稿タイプを簡単に拡張するコード

以下のコードを functions.php などに貼りつけて下さい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class wbCustom
{
    function custom_post_type($title, $slug, $supports, $capability_type = 'post', $menu_position = 30)
    {
        $args = Array(
            'labels' => Array(
                'name' => _x($title, 'post type general name'),
                'singular_name' => _x($title, 'post type singular name'),
                'add_new' => _x($title . 'を追加', 'book'),
                'add_new_item' => __('新しい' . $title . 'を追加'),
                'edit_item' => __($title . 'を編集'),
                'new_item' => __('新しい' . $title),
                'view_item' => __($title . 'を編集'),
                'search_items' => __($title . 'を探す'),
                'not_found' =>  __($title . 'はありません'),
                'not_found_in_trash' => __('ゴミ箱に' . $title . 'はありません'),
                'parent_item_colon' => ''
            ),
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => $capability_type,
            'hierarchical' => false,
            'menu_position' => $menu_position,
            'has_archive' => true,
            'supports' => $supports
        );
        register_post_type($slug, $args);
    }
 
    function custom_taxonomies($taxName, $taxSlug, $postType) {
        register_taxonomy(
            $taxSlug,
            $postType,
            array(
                'hierarchical' => true,
                'label' => $taxName,
                'singular_name' => $taxName,
                'query_var' => true,
                'rewrite' => true
            )
        );
    }
 
    function custom_post_dashboard($custom_post_type)
    {
        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>';
    }
}

カスタム投稿タイプを拡張する

続いて拡張に移ります。このコードも functions.php に書きます。

カスタム投稿タイプを作成する

3つのカスタム投稿タイプを追加する場合以下のように書きます。wbCustom::custom_post_type() の第一引数は投稿タイプ名、第二匹数が投稿タイプスラッグ、第三引数は使用する項目です。この場合はタイトルとエディタが使用できます。

1
2
3
4
5
6
7
function custom_post_type_set()
{
    wbCustom::custom_post_type('お知らせ', 'info', Array('title', 'editor'));
    wbCustom::custom_post_type('製品情報', 'item', Array('title', 'editor'));
    wbCustom::custom_post_type('スタッフ', 'staff', Array('title', 'editor'));
}
add_action('init', 'custom_post_type_set');

カスタムタクソノミーを作成する

以下のように wbCustom::custom_taxonomies() 第一引数はタクソノミー名、第二匹数はタクソノミーのスラッグ、第三引数は設定したい投稿タイプ、例えば post とか入れます。

1
2
3
4
5
6
function custom_taxonomies_set()
{
    wbCustom::custom_taxonomies('色', 'color', 'item');
    wbCustom::custom_taxonomies('サイズ', 'size', 'item');
}
add_action('init', 'custom_taxonomies_set', 0);

カスタム投稿タイプの記事数をダッシュボードに表示

デフォルトで投稿やページの数がダッシュボードで確認できますが、カスタム投稿タイプは確認できませんが、以下のコードを使えば表示することができます。

1
2
3
4
5
6
7
function custom_post_dashboard_set()
{
    wbCustom::custom_post_dashboard('info');
    wbCustom::custom_post_dashboard('item');
    wbCustom::custom_post_dashboard('staff');
}
add_action('right_now_content_table_end', 'custom_post_dashboard_set');

おわりに

昔書いたコードなので上手く動くか心配ですが、問題があればご連絡ください。今回の記事は、自らの過去記事を参考にしました。

コメント

  1. Taiさんのコメント

    なるほどー、クラスにしちゃうんですね! これは便利そうです。こんど使わせてもらいます。

    ところで、__()とか_x()とかは翻訳用の関数なので、なくてもだいじょーぶですよ:-)

    翻訳用に入れているのだとしても、テキストドメインがなかったり、変数をはさんでるところのフォーマットがちょっと違ってたりしてるので、たぶん翻訳できません。

    詳しくは
    http://wpdocs.sourceforge.jp/I18n_for_WordPress_Developers
    を参照してみてください。

    • webleさんのコメント

      Tai さん、はじめまして。このブログを書いてる weble と申します。昔から WordPress のサイトカスタマイズの時は参考にさせて頂いております。WordPress のテーマを初めてカスタマイズする時は Tai さんのカスタマイズの記事を何度も読み返していました^^。

      翻訳用の関数の件、ご指摘頂きありがとうございます。考えてみれば、他のサイトを参考に作ってきた関数で中身の部分まで気にしていませんでした。変に書き換えて動かなくなるのも心配なので、時間を見つけてもう少し簡素化して追記させて頂きますね。参考 URL もありがとうございます。

  2. b2iさんのコメント

    バッチリ動きました!ありがとう!
    (WordPress 3.2.2@twenty eleven)

    • webleさんのコメント

      動作検証頂きありがとうございます。また分からないことがあれば、ご活用ください^^。