WordPress 3.0 からはカスタム投稿タイプというものが実装されました。
扱いは詳しく説明されている方のおかげでとても簡単です。以下のページが参考になりました。今回はカスタム投稿タイプの設定方法を改めて、成功事例ということで紹介しておきます。
カスタム投稿タイプ(Custom Post Type)の導入と使い方 [WordPress 3.0] | Odysseygate.com
WordPress 3.0のカスタム投稿タイプ機能(その1) – The blog of H.Fujimoto
投稿タイプの追加
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 | function 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' => 2, 'supports' => array('title','editor', 'custom-fields') ); register_post_type('project',$args); } add_action('init', 'custom_post_type'); |
たったこれだけで投稿タイプを増設できます。
ちなみに私のこのブログの右側プロジェクトは以上のコードで設定しています。
追加した投稿タイプを読み込み
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $loop = new wp_query(); $loop->query('post_type=project&orderby=meta_value&meta_key=progress&order=DESC&showpost=100'); while($loop->have_posts()) { $loop->the_post(); $progress = get_post_custom_values('progress'); if($progress[0] < 100) { echo '<section>'; echo '<h2>' . get_the_title() . '</h2>'; echo apply_filters('the_content', get_the_content()); echo '<div class="progressbar">'; echo '<div class="value" style="width:' . abs($progress[0]) . '%;">'; echo '<div class="text">' . abs($progress[0]) . '%</div>'; echo '</div>'; echo '</div>'; echo '</section>'; } } |
私のブログ右側の読み込み部分です。読み込み時にカスタムフィールドの値でソートをしています。
ただ読み込むだけであれば、実際はもっと簡略化することができますが、ただノーマルなコードを紹介しても意味がありませんので、カスタムフィールドを駆使した応用例を紹介しておきます。
[...] [...]