3

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

2

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="<?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

  • I could not figure out how to make the url work the way you have it set up. I am not sure what to put in for category base. I ended up with www.url.com?cat=9&m=<?php echo $year.$month;?> it registers in the address bar as www.url.com/2014/04?cat=9 I would like to use the url that you have listed though. Do you have any hints for me? Thank you! – tylerlindell Sep 13 '14 at 15:05
1

Take a look at Scribu's Smart Archives Reloaded plugin which allows you to display a list of posts grouped by year and month.

And to limit the archives by category is as simple as this:

smart_archives( '', 'category_name=category_name' ); 
0

Perhaps use query_posts to generate your query and then use a custom loop.

http://codex.wordpress.org/Function_Reference/query_posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.