2012年1月
データの移行に伴い、phpコードの文字化けが発生している箇所があります。
&lti や > のような文字化けが出ているコードはコピーしないでください。
現在、手直し中です。すみません。

月別アーカイブにカスタムポストを含めて表示する

通常、月別アーカイブには、カスタムポストの記事タイトルは表示されません。

カスタムポストも頻繁に更新している場合、通常の投稿記事のタイトルと一緒にアーカイブ表示した方が便利ですよね。

カスタムポストの記事タイトルを通常の投稿記事と一緒にアーカイブに含める方法の一つに、次のような書き方があります。

たとえば、カスタムポスト・タイプに「diary」「love」を設定している場合、query_postの関数の中で、post_type として追加します。  

  <?php
        global $wpdb;
		$cdate = date('Y-m-d H:i:s');
        $years = $wpdb->get_results("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= \"$cdate\" and post_type = 'post' ORDER BY post_date DESC");
	if($years)
		{
			foreach($years as $years_obj)
			{
				$year = $years_obj->year;
				$month = $years_obj->month;
				?>
      <?php query_posts(array('post_type' => array('post','diary','love'),'showposts' =>100, 'monthnum' => $month, 'year' => $year )); ?>
      <div class="●●">
        <h3> <?php echo  date('F', mktime(0,0,0,$month,1)).'-'. $year; ?> </h3>
        <ul>
          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
          <li> <a href="<?php the_permalink() ?>">
            <?php the_title(); ?>
            </a> - <span class="arclist_date">
            <?php the_time('M j日 ') ?>
            </span> </li>
          <?php endwhile; endif; ?>
        </ul>
      </div>
      <?php
			}
		}
		 ?>



こちらは、姉妹サイト「sanmarie*com」の月別アーカイブの実例。

ちょっと分かりにくいですが、メインの映画・音楽レビューのリストの中に子育てネタや恋愛ネタのタイトルも入ってます。

月別アーカイブ カスタムポスト



上記のコードは私が使っているテーマ・ファイルの月別アーカイブの記述をベースに変更を加えたものなので、他のテーマでも上手く動作するか分かりませんが、興味のある方は一度試してみてください。



◆追記◆

こういうやり方でも出来るみたいです。

function.phpに記入。

function add_custom_types_archive( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
 'post', 'your-custom-post-type-here'
            ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'add_custom_types_archive' );

Ads

Leave a Reply

Trackback URL

QR Code Business Card