Programming
Archives for a category WordPress plugin
For the new version of this website I wanted to show a list of monthly archives, limited to posts of a specific category. The software used to run this website is WordPress but the wp_get_archives() function, to get a list of archives, does not have the ability to filter on category. So I created the ‘Archives for a category’ plugin that enhances this function to show archive links for a specific category.
Installation
- Download the plugin zip file and unzip the file in a temporary directory.
- Put the
kwebble_archives_by_cat.phpfile in your WordPress plugin directory:
<your wordpress dir>/wp-content/plugins - Activate the plugin ‘Archives for a category’ on the Plugin Management page of the WordPress administration panel.
- Optionally disable the canonical URLs from the menu Options | Kwebble.
A WordPress feature added in version 2.3, called canonical URLs, redirects browsers on certain URLs. This also happens with the URLs for the archives with a cat parameter. This causes the archive pages to contain posts which do not belong to the selected period.To solve this problem the plugin can disable canonical URLs. This uses the technique used in the Disable Canonical URL Redirection plugin Mark Jaquith made. So if you are already using that plugin you don’t need to change the setting for this plugin.
To disable canonical URLs go to the administration section of your blog, choose the Options tab and then the Kwebble subtab. On that page you will find a checkbox to disable canonical URLs.
Usage
After installing and activating the plugin the wp_get_archives() function accepts a ‘cat’ parameter. Its value must be one or more category ID’s, separated by comma’s, of the
categories to include in building the list of archives.
For example, to show the default monthly list of archives for category 1 put this in a template:
<?php wp_get_archives('cat=1'); ?>
The same list, but with posts from categories 1 and 3:
<?php wp_get_archives('cat=1,3'); ?>
To create a list of archives for category 1 as a dropdown box use:
<?php wp_get_archives('format=option&cat=1'); ?>
For a complete description of all parameters see the documentation of wp_get_archives().
The last thing is to make sure the template used to show each archive displays posts from the selected category. I’m using category specific templates on this site, like category-id.php where id is the ID of the category to display.
You can use other templates in the template hierarchy, but make sure the template shows items of that category.
Limitations
This plugin does not work for weekly archives. The list with archive links is correct, but the links themselves do not include the category. So when used, WordPress will not filter the resulting page on the category. The technical reason is that WordPress does not apply filters when the links for weekly archives are generated, so the plugin can’t change them. Perhaps in a next version of WordPress…
This plug-in was developed and tested to work correctly with WordPress versions 2.2.1 and 2.3.1. It probably also works with 2.3.2.
Older versions
Version 1.2
23-11-2007 - Added support for WordPress 2.3.1. Download.
Version 1.0
15-08-2007 - Initial version, works with WordPress 2.2.1. Download.
Copyright
Copyright 2007, 2008 Rob Schlüter. All rights reserved.
Licensing terms
- You may use, change and redistribute this software provided the copyright notice above is included.
- This software is provided without warranty, you use it at your own risk.
Leave a response
This is where you can leave your response. All fields are required but the e-mail address will not be published.
Responses
Queenvictoria
4 october 2007, 22:08
thanks kwebble for this . here is the update to make it work with the v2.3 version of wordpress .
function kwebble_getarchives_where_for_category($where, $args){
global $kwebble_getarchives_data;
if (isset($args['cat'])){
// Preserve the category for later use.
$kwebble_getarchives_data['cat'] = $args['cat'];
if ( get_bloginfo('version') > 2.3 )
$where .= ' AND post2cat.category_id=' . $args['cat'];
else
$where .= " AND taxonomy.taxonomy = 'category' AND taxonomy.term_id = " . $args['cat'];
}
return $where;
}
and later
function kwebble_getarchives_join_for_category($join, $args){
global $wpdb;
if (isset($args['cat'])){
if ( get_bloginfo('version') > 2.3 )
$join .= ' JOIN ' . $wpdb->post2cat . ' post2cat ON post2cat.post_id=ID';
else
$join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = taxonomy.term_taxonomy_id) ";
}
return $join;
}
Queenvictoria
4 october 2007, 22:14
sorry that should be less than not greater than 2.3
function kwebble_getarchives_where_for_category($where, $args){
global $kwebble_getarchives_data;
if (isset($args['cat'])){
// Preserve the category for later use.
$kwebble_getarchives_data['cat'] = $args['cat'];
if ( get_bloginfo('version')
and later
function kwebble_getarchives_join_for_category($join, $args){
global $wpdb;
if (isset($args['cat'])){
if ( get_bloginfo('version') post2cat . ' post2cat ON post2cat.post_id=ID';
else
$join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = taxonomy.term_taxonomy_id) ";
}
return $join;
}
Rob Schlüter
5 october 2007, 20:00
Thanks, I didn’t realize the plugin needed an upgrade for the new Wordpress version.
I’ll test this when I upgrade my site to the 2.3 version and if everything is ok I will post a new version of the plugin.
Mike
31 october 2007, 20:22
Just installed the plugin myself (within WP v2.3.1) and it needed queenvictoria’s modification.
However, if you are going to try it yourself: just copy & paste the code from queenvictoria’s FIRST post (from “4 october 2007, 22:08) and then modify the ‘greater than’ symbol to a ‘less than’ by hand in BOTH instances.
It looks as if the code got messed up when submitting the second post (from “4 october 2007, 22:14″)
Rob Schlüter
1 november 2007, 9:28
Thanks for the note.
In the coming days I’m upgrading my development version of the site to WordPress 2.3.1. Then, if everything works ok, I’ll upgrade my site and make a new version of this plugin available.
jay
20 november 2007, 16:11
If you’re going to use this with WordPress 2.3, you’ll have to use the plugin which disables canonical redirects. The new version of Wordpress doesn’t correctly process the URLs to show archives only in a given cat. (Specifically URLs like /2007/10/?cat=1 won’t work unless canonical URLs are disabled.)
Here’s the plugin…
http://txfx.net/files/wordpress/disable-canonical-redirects.phps
Rob Schlüter
21 november 2007, 21:58
At the moment I’m testing a new version of the plugin and also noticed that the URL’s generated by the plugin are redirected incorrectly creating archive pagess with incorrect posts.
To solve this I’m adding the possibility to disable canonical URLs from the administration menu.
Rob Schlüter
23 november 2007, 23:30
A new version (1.2) with WordPress 2.3 support is now available.
WordPress 2.3 compatiblity Plugins | Dreamer’s Blog
6 december 2007, 23:37
[…] Archives for a category (http://kwebble.com/blog/2007_08_15/archives_for_a_category) 1.2 […]
WordPress Plugins Database » Plugin Details » Archives for a category
21 december 2007, 13:42
[…] Visit […]
stephen
29 december 2007, 10:12
This is really great, but I was wondering if there was a way to show multiple categories.
Rob Schlüter
30 december 2007, 23:57
Using multiple categories is not possible at the moment. I’ll have a look to see if I can add support for more than 1 category.
steve
1 januari 2008, 23:24
That would be great!
Rob Schlüter
6 januari 2008, 14:29
Just updated the plugin to version 1.3, which adds support for multiple categories.
jack
22 januari 2008, 2:32
exactly what I needed, thanks!
Tony
23 januari 2008, 4:34
Hi, this was exactly what I was looking for! And using it along with the category-id.php works very well! Thank you so much!
cj
25 februari 2008, 23:33
Hi, love the plug-in, it works great. One question though, is there a way to make the monthly category archives use the archive template instead of the category template? Thanks
Rob Schlüter
26 februari 2008, 13:19
cj, perhaps you could rename the category template and create a new one that checks if a category or archive page is requested, and
include()the appropriate template.I think you’ll have check for a part in the URL that identifies the requested page type, for example a date pattern, because the
is_archive()is also true for category pages, according to the Codex .2pt3 › archive » wordpress tips
28 februari 2008, 0:07
[…] *archives for category* this is essential if you’ve got a category for blog posts, but you’ve also got posts for other items. this way you can add cat=X to any get_wp_archives(). […]
AWSOM.org = Artist Website Setup Options Markup » Blog Archive » AWSOM Archive 1.4.0 released
9 march 2008, 23:44
[…] plugin adds in a much requested category limiting feature that works in conjunction with the “Archives for a Category” plugin to allow your archive to show only entries from specific categories. (Note, you MUST […]
slambert
11 march 2008, 6:46
this is great. Curious if it’s possible to show the number of posts next to the month and year. for example August 2007 (6) If there were 6 posts from that month.
Rob Schlüter
14 march 2008, 21:37
@slambert: does the parameter show_post_count you can use with wp_get_archives do what you need?
slambert
16 march 2008, 23:55
@rob Schülter: yes and no. That’s the functionality, but I still want to filter it by category. Make sense?
Zef
13 april 2008, 0:41
Just posting to say thank you SO much for creating this plugin. I’ve had a day of searching and you have saved my website!
Archives for a category WordPress plugin
25 april 2008, 16:50
[…] Archives for a category WordPress plugin Uncategorized […]
Tom
28 april 2008, 20:07
I just noticed that this plugin doesn’t work in Wordpress 2.5. Any plans to update it? Thanks for your work so far — it’s a very helpful plugin.
Rob Schlüter
28 april 2008, 22:01
I will investigate what’s the reason 2.5 breaks the plugin.
WordPress MyManual » Blog Archive » Archives for a category
13 may 2008, 2:34
[…] Archives for a category ??????????????????????????????wp_get_archives() ??????????????????????????????????????? […]