このブログもワードプレスでできていますが、ワードプレスってすごく便利ですよね!バージョン1.5か2.0ぐらいから使い始めておりますが、この機能の進化に目をみはる物がありますね。バージョンMUと統合されて更に使いやすくなったワードプレス!
ウェブ制作にもCMSとしてよく使われていますね。ほんとワードプレスすごい!PHPの学習する際にもこのコードなどを参考にしても良いかもしれませんね。
3.0からの新機能で便利な機能としてカスタム投稿をご紹介します。プラグインでも利用可能ですが、functions.phpに記述しても使えます。
まずカスタム投稿って何?って事ですよね。
スポンサーリンク
管理画面で「投稿」って項目がありますが、基本的にそこから記事を投稿します。当たり前ですが。。。まあ、ブログと同じですね。
その記事と違うジャンルの記事を書きたいなと思ったときは普通はカテゴリーで分けて投稿しますよね。ただ管理する側だとわかりづらくなりますし、いろんな記事がごちゃごちゃしちゃうのでわかりやすくこの「投稿」って項目を増やそうということができるのです!
カテゴリー別で投稿記事を各ページに表示することは可能ですが、記事ごとに管理できた方がわかりやすいですよね。
要するに複数の「投稿」を作ろうって事なんですね。
下記をそのままfunction.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 | function add_custompost_type() { $arg = array( 'label' => 'カスタム投稿', 'labels' => array( 'singular_name' => 'カスタム投稿', 'add_new_item' => '新規投稿を追加', 'add_new' => '新規追加', 'new_item' => '新規投稿', 'view_item' => 'カスタム投稿を表示', 'not_found' => 'カスタム投稿は見つかりませんでした', 'not_found_in_trash' => 'ゴミ箱にカスタム投稿はありません。', 'search_items' => 'カスタム投稿を検索', ), 'public' => true, 'hierarchical' => false, 'menu_position' => 5, 'supports' => array('title','editor','author','thumbnail', 'excerpt','comments','custom-fields' ,'revisions'), 'has_archive' => true ); register_post_type('custom', $arg); //カテゴリ $arg = array( 'label' => 'カテゴリ', 'labels' => array( 'name' => 'カテゴリ', 'singular_name' => 'カテゴリ', 'search_items' => 'カテゴリを検索', 'popular_items' => 'よく使われているカテゴリ', 'all_items' => 'すべてのカテゴリ', 'parent_item' => '親カテゴリ', 'edit_item' => 'カテゴリの編集', 'update_item' => '更新', 'add_new_item' => '新規カテゴリを追加', 'new_item_name' => '新しいカテゴリ', ), 'public' => true, 'hierarchical' => true, ); register_taxonomy('custom_category', 'custom', $arg); //タグ $arg = array( 'label' => 'タグ', 'singular_label' => 'タグ', 'update_count_callback' => '_update_post_term_count', 'public' => true, 'hierarchical' => false, 'show_ui' => true ); register_taxonomy('custom_tag', 'custom', $arg); flush_rewrite_rules(); } add_action('init', 'add_custompost_type'); |
カスタム投稿を出力する側の記述です。
home.phpやindex.phpに記述するとわかりやすいかも。
1 2 3 4 | <?php $custom = new WP_Query(array( 'post_type' => 'custom'));?> <?php while($custom->have_posts()) : $custom->the_post(); ?> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>(<?php the_time('Y年m月d日 H時i分'); ?>)</p> <?php endwhile; ?> |
カスタム投稿・分類に関して少し情報を残しておきます。意外にカスタム分類って、私がやろうと思ったやり方なんですが、結構面倒だな~と思いましたね。これは使い方次第ですね。
まず上記の例で作りますと、カスタム投稿のポストタイプがcustomでそのカテゴリーがcustom_categoryですね。
それでそのカテゴリーを色にしましょうか。赤色(red)、青色(blue)、黄色(yellow)で分けて登録したとします。各カテゴリーは独立していて混ざらない条件にします。
普通の投稿のカテゴリー一覧ページのcategory.phpをtaxonomy.phpに変更して作成します。
このtaxonomy.phpがカスタム投稿のカテゴリー一覧ページになります。
1 2 3 4 5 6 7 8 | <?php $term = get_term_by('slug', get_query_var('term') ,get_query_var('taxonomy'));?> <?php if($term->slug === 'red'): ?> <h2>赤色</h2> <?php elseif($term->slug === 'blue'):?> <h2>青色</h2> <?php elseif($term->slug === 'yellow'):?> <h2>黄色</h2> <?php endif;?> |
まずはタイトル変更、上記のように記述するとそれぞれのカテゴリーで表示が変わります。
1 2 | <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();?> |
の上にカスタムカテゴリーのみ表示するように下記を追加します。
1 2 | <?php global $wp_query; query_posts(array_merge(array( 'post_type' => 'custom' ),$wp_query->query));?> |
それでサイドバーをカスタム投稿用に作成しますsidebar-2.phpとして
1 | <?php get_sidebar(2); ?> |
で呼び出します。
まずは最近の投稿を取得します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php $taxonomy_name = 'custom'; $numberposts = 5; if(is_tax($taxonomy_name,'red')): $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => 'coo', 'numberposts' => $numberposts)); elseif(is_tax($taxonomy_name,'blue')): $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => 'officer', 'numberposts' => $numberposts)); elseif(is_tax($taxonomy_name,'yellow')): $tax_posts = get_posts(array('post_type' => get_post_type(), 'taxonomy' => $taxonomy_name, 'term' => 'info' ,'numberposts' => $numberposts)); endif; if($tax_posts): ?> <li><h3>最近の投稿</h3> <ul> <?php foreach($tax_posts as $tax_post): ?> <li><a href="<?php echo get_permalink($tax_post->ID); ?>"><?php echo esc_html($tax_post->post_title); ?></a></li> <?php endforeach; ?> </ul> <?php endif; ?> </li> |
こんな感じで書いたらよいです。
カテゴリーは
1 | <?php wp_list_categories('taxonomy=custom&show_count=1&title_li='); ?> |
でカスタム投稿のカテゴリーすべて出力されますが、同一カテゴリーのみの出力は以下の通り
1 2 3 4 5 6 7 8 | <ul> <?php $terms = get_the_terms($post->ID, 'custom'); foreach ($terms as $term) : ?> <li><a href="<?php echo get_term_link($term, 'custom'); ?>"><?php echo $term->name; ?></a>(<?php echo $term->count; ?>)</li> <?php endforeach; ?> </ul> |
月別アーカイブ表示ですがこれだけはカテゴリー別表示できず。。。。カスタム投稿すべての表示になってしまいます。
functions.phpに以下記述した上で
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | global $my_archives_post_type; add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 ); function my_getarchives_where( $where, $r ) { global $my_archives_post_type; if ( isset($r['post_type']) ) { $my_archives_post_type = $r['post_type']; $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where ); } else { $my_archives_post_type = ''; } return $where; } add_filter( 'get_archives_link', 'my_get_archives_link' ); function my_get_archives_link( $link_html ) { global $my_archives_post_type; if ( '' != $my_archives_post_type ) $add_link .= '?post_type=' . $my_archives_post_type; $link_html = preg_replace("/href=\'(.+)\'\s/","href='$1".$add_link."'",$link_html); return $link_html; } |
sidebar-2.phpに下記を記述します。
1 | <?php wp_get_archives(array('type' => 'monthly', 'post_type' => 'blog', 'show_post_count' => true)); ?> |
カスタム投稿のカテゴリー別表示がわかったら更新しておきます。
あとはsingle.phpですが、カスタム投稿用のsingle-custom.phpを作成しておきます。
1 2 | <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> |
の上にこれを記述します。
1 2 3 4 5 6 7 8 | <?php $c = 'custom'; if(is_object_in_term($post->ID, $c, 'red')):?> <h2>赤色</h2> <?php elseif(is_object_in_term($post->ID, $c, 'blue')):?> <h2>青色</h2> <?php elseif(is_object_in_term($post->ID, $c, 'yellow')):?> <h2>黄色</h2> <?php endif;?> |
そして同一カテゴリーでの前後リンクはプラグインで補います。
http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/
このプラグイン英語で書かれているのですが、以下の通り記述しても良いと思います。
1 2 | <?php previous_post_link_plus(array('in_same_cat' => true,'in_same_tax' => true)); ?> <?php next_post_link_plus(array('in_same_cat' => true,'in_same_tax' => true)); ?> |
これでいけます!
因みにカスタム投稿をダッシュボードに表示する場合は下記をfunctions.phpに書いたらOK!
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 | function custom_post_dashboard() { $dashboard_custom_post_types= Array( 'custom', 'sonohoka' ); foreach($dashboard_custom_post_types as $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>'; } } add_action('right_now_content_table_end', 'custom_post_dashboard'); |
今回はカスタム投稿ポストタイプをcustomってしましたが各自ポストタイプを変更したら該当箇所も変更して下さいね。
あとカスタム投稿と関係ないのですがカテゴリー別に出力できる方法を覚え書き用として残しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $cate_post = new WP_Query(array( 'category_name' => 'new', 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' )); if ($cate_post ->have_posts()) : ?> <p>新着記事</p> <ul> <?php while($cate_post ->have_posts()) : $cate_post ->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>(<?php the_time('Y年m月d日 H時i分'); ?>)</li> <?php endwhile; ?> </ul> <?php else : ?> <p>新着記事はありません</p> <?php endif; ?> </div> |
ついでに同一カテゴリーでの前後リンクsigle.phpのしたにあるやつですね
1 2 | <?php previous_post_link('« %link', '%title', TRUE, ''); ?> <?php next_post_link('%link »', '%title', TRUE, ''); ?> |
それからカテゴリー別に月別アーカイブ表示
Archives for a categoryってプラグイン必須ですが、設定すれば下記の記述でOK
1 | wp_get_archives(array('type' => 'monthly', 'show_post_count' => true, 'cat' => 1)); |
それからカテゴリー別最近の投稿表示
1 | wp_get_archives(array('type' => 'postbypost', 'cat' => 1, 'limit' => 5)); |
ま~読みづらいかもしれませんが単純に覚え書きですのでご容赦を。。。
1 2 3 4 5 6 7 8 9 | remove_action( 'wp_head', 'feed_links_extra', 3 ); remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' ); remove_action( 'wp_head', 'index_rel_link' ); remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); remove_action( 'wp_head', 'wp_generator' ); |
functions.phpに上記は必須かな。結構いらない表示が多いのでコレで省きます。