I’ve been pulling my hair out on this one – I need to hard code in wp_get_archives()
for a specific category into a template.
This post was useful:
Category Specific Archive
but, as with all the plugins I’ve found so far, they don’t work with WP 3.1 (or you need to hack the core files which I obviously don’t want to do).
Is there another solution I’m not aware of? Maybe a method by creating a separate loop?
THanks for any pointers
osu
3 Answers
I did this in a relatively complicated way using a custom query. I did not know about Smart Archives Reloaded so I wrote code myself. But it works. Replace your category ID for “term_taxonomy.term_id”
<?php
global $wpdb, $wp_locale;
$query = "select YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts from $wpdb->posts, $wpdb->term_taxonomy, $wpdb->term_relationships
WHERE $wpdb->posts.post_status="publish"
AND $wpdb->posts.post_type="post"
AND $wpdb->term_taxonomy.term_id = 11
AND $wpdb->posts.ID = $wpdb->term_relationships.object_id
AND $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
$arcresults = $wpdb->get_results($query);
foreach ($arcresults as $arcresult):
$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);?>
<li><a href="https://wordpress.stackexchange.com/questions/13968/<?php bloginfo("url') ?>/[your_category_base]/[your_category_name]/date/<?php echo $arcresult->year; ?>/<?php echo str_pad($arcresult->month, 2, '0', STR_PAD_LEFT); ?>"><?php echo $text; ?> </li>
<?php endforeach; ?>
Writing this I was helped by looking at http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/general-template.php where wp_get_archives is defined.
And used the code found here (put in functions.php):
http://snipplr.com/view.php?codeview&id=17432
to create permalinks for archives for one category in the form of
http://example.com/category_base/category_name/date/YYYY/MM