Skip to content

Instantly share code, notes, and snippets.

@torounit
Created March 7, 2013 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save torounit/5106240 to your computer and use it in GitHub Desktop.
Save torounit/5106240 to your computer and use it in GitHub Desktop.
Wordpressで指定したカテゴリーの年別アーカイブを作成する | webOpixel http://www.webopixel.net/wordpress/236.html の改造版。プラグイン有効時にflush_rules()か、パーマリンク設定の更新が必要。
<?php
function extend_date_archives_add_rewrite_rules() {
add_rewrite_rule( 'category/([^/]+)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&feed=$matches[4]', 'top' );
add_rewrite_rule( 'category/([^/]+)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?category_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]', 'top' );
add_rewrite_rule( 'category/([^/]+)/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?category_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]', 'top' );
add_rewrite_rule( 'category/([^/]+)/date/([0-9]{4})/?$', 'index.php?category_name=$matches[1]&year=$matches[2]', 'top' );
}
add_action('wp_loaded', 'extend_date_archives_add_rewrite_rules');
function extend_date_wp_title ($title , $sep, $seplocation) {
if(is_date()){
global $wp_query;
$cat = get_category( $wp_query->query_vars["cat"]);
if( $seplocation == "right" ){
$title = $title." ".$cat->name." ".$sep." ";
}else {
$title = " ".$sep." ".$cat->name." ".$title;
}
}
return $title;
}
add_filter('wp_title','extend_date_wp_title', 100 ,3 );
function wp_get_category_archive($cat_ID) {
global $wpdb;
$results = $wpdb->get_results($wpdb->prepare("
SELECT YEAR(post_date) AS post_year
FROM $wpdb->posts p, $wpdb->term_relationships tr, $wpdb->term_taxonomy tt, $wpdb->terms t
WHERE p.ID = tr.object_id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.term_id = t.term_id = $cat_ID
AND p.post_status = 'publish'
AND p.post_type = 'post'
GROUP BY post_year
ORDER BY post_year DESC
"));
$cat = get_category( $cat_ID );
foreach ($results as $result) :?>
<li><a href="<?php bloginfo('url'); ?>/category/<?php echo $cat->slug;?>/date/<?php echo $result->post_year; ?>"><?php echo $result->post_year; ?>年</a></li>
<?php endforeach; ?>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment