カスタム投稿タイプで
直近の●●件
カテゴリー一覧
月別・年度別アーカイブ
を表示したいとき
- パーマリンクの設定はデフォルト
- カスタムポストのカテゴリーはtaxonomy追加で、”カスタムポスト名_cat”
こんな感じのページが出来ます。
http://halu-g.jp/?post_type=report
sidebar.phpとかに
//直近の5件
<ul>
<?php
global $post;
$post_type = get_post_type();
$args = array (
'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
'numberposts' => 5
);
$my_posts = get_posts( $args );
foreach ($my_posts as $post) :
setup_postdata( $post );
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
<p>カテゴリー</p>
<ul>
<?php wp_list_categories(array('title_li'=>'', 'taxonomy'=>$post_type.'_cat', 'show_count'=>1)); ?>
</ul>
<p>月別アーカイブ</p>
<ul>
<?php
add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
add_filter( 'get_archives_link', 'my_get_archives_link', 10, 2 );
//直近の12ヶ月分
$my_archive = wp_get_archives('type=monthly&post_type='.$post_type.'&show_post_count=1&limit=12&echo=0');
//年と月を別のフォーマットにかえたいとき利用
//$my_archive = str_replace( array("年"), '.', $my_archive );
//$my_archive = str_replace( array("月"), '', $my_archive );
echo $my_archive;
?>
</ul>
<p>年間アーカイブ</p>
<ul>
<?php
echo wp_get_archives('type=yearly&post_type='.$post_type.'&show_post_count=1');
remove_filter( 'getarchives_where', 'my_getarchives_where');
remove_filter( 'get_archives_link', 'my_get_archives_link' );
?>
</ul>
</pre><p><br />function.php</p><pre>//++++++++++++++++++++++++++++++++++++++++++++++
//カスタム投稿のアーカイブ
//
function my_getarchives_where( $where, $r ) {
global $my_archives_post_type;
$my_archives_post_type =get_post_type();
$my_archives_post_type = get_post_type();
if ( isset($r['post_type']) ) {
$my_archives_post_type = $r['post_type'];
$where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
}
return $where;
}
//
// ?post_type=任意の名前(パラメーター) を追加する
//
function my_get_archives_link( $link_html ) {
global $my_archives_post_type;
$my_archives_post_type = get_post_type();
if ($my_archives_post_type != '') {
$add_link = '&post_type=' . $my_archives_post_type;
$link_html = preg_replace('@</a>(.+?)</li>@', '年</a>$1</li>', $link_html);
}
return $link_html ;
}