情報を追記している場合はありますが、古い情報を訂正はしていませんので、公開年月日を参照してください。プラグイン・タグ、いずれもワードプレス・PHPのバージョン等によって動作しない場合もあります。
カテゴリー別の月アーカイブの表示は Archives for a category というプラグインを使用していましたが、あるサイトで、これが急に動作しなくなってしまい、
http://qiita.com/shuhei@github/items/c364c0ea129a06e9f71a のコードを使用してみました。が、やはりダメ。
で、http://takenotes.jp/2013/08/07/wordpress-category-monthly-archives/ のコードを使用したところ、アーカイブのページはきちんと表示されるのだけど、そのカテゴリーの投稿がない月もリストに出てしまい、後者のコードのURL書き換え部分を前者のコードに差し込んで、
add_filter('getarchives_where', 'custom_archives_where', 10, 2);
add_filter('getarchives_join', 'custom_archives_join', 10, 2);
function custom_archives_join($x, $r) {
global $wpdb;
$cat_ID = $r['cat'];
if (isset($cat_ID)) {
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
} else {
return $x;
}
}
function custom_archives_where($x, $r) {
global $wpdb;
$cat_ID = $r['cat'];
if (isset($cat_ID)) {
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($cat_ID)";
} else {
$x;
}
}
function wp_get_cat_archives($opts, $cat) {
$args = wp_parse_args($opts, array('echo' => '1')); // default echo is 1.
$echo = $args['echo'] != '0'; // remember the original echo flag.
$args['echo'] = 0;
$args['cat'] = $cat;
$archives = wp_get_archives(build_query($args));
$archs = explode('</li>', $archives);
$links = array();
$cat0 = get_the_category();
$cat_slug = $cat0[0]->category_nicename;
foreach ($archs as $archive) {
$link = preg_replace("//date//", "/{$cat_slug}/date/", $archive);
array_push($links, $link);
}
$result = implode('</li>', $links);
if ($echo) {
echo $result;
} else {
return $result;
}
}
で、万事丸く収まりました。
<?php $cat_ID = 7; wp_get_cat_archives('type=monthly&limit=12', $cat_ID); ?>
*別の環境では Archives for a category がちゃんと動きます。