以前の記事で同様なことを記事にさせていただいておりましたが、紹介していた方法では指定されているカテゴリに該当記事がないのにもかかわらずアーカイブリンクが出力されてしまうという現象が起きていました。その現象を解消する方法を紹介いたします。

function.phpに記載しているタグを

[php]
function extend_date_archives_flush_rewrite_rules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action(‘init’, ‘extend_date_archives_flush_rewrite_rules’);
function extend_date_archives_add_rewrite_rules($wp_rewrite) {
$rules = array();
$structures = array(
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
$wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
);
foreach( $structures as $s ){
$rules += $wp_rewrite->generate_rewrite_rules($s);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action(‘generate_rewrite_rules’, ‘extend_date_archives_add_rewrite_rules’);
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\//", "/category/{$cat_slug}/date/", $archive);
array_push($links, $link);
}
$result = implode(‘</li>’, $links);

if ($echo) {
echo $result;
} else {
return $result;
}
}
[/php]

以下のコードにゴソッと入れ替える(初めて入れる方はこちらのコードを入れる)

[php]
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) {

$child_cats = get_term_children($cat, ‘category’);
$child_cats[] = $cat;
$cat_id = implode(‘,’, $child_cats);

$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_id;

$tag = ($args[‘format’] === ‘option’) ? ‘option’ : ‘li’;

$archives = wp_get_archives(build_query($args));
$archs = explode(‘</’.$tag.’>’, $archives);
$links = array();

foreach ($archs as $archive) {
$link = preg_replace("/='([^’]+)’/", "=’$1?cat={$cat}’", $archive);
array_push($links, $link);
}
$result = implode(‘</’.$tag.’>’, $links);

if ($echo) {
echo $result;
} else {
return $result;
}
}
[/php]

出力方法は以前と同じで以下の通り

[php]
//IDを指定
$cat_ID = 0;
wp_get_cat_archives(‘type=monthly’, $cat_ID);
[/php]

参考:wp_get_archivesっぽいのでカテゴリ&月別アーカイブ(子孫カテゴリ込み)
http://doshiroutonike.com/web/wordpress/wp-category/1686/

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です