Date archive for a category
There are a few ideas out there How to build date archive for a category. I am joining the race now.
Some of us, wordpress developers, may think this is hacky but I bet a few will like it this way because this works!.
This idea would end up in url like this
/archives/category/{category-slug}/yyyy/mm/Lets get started and you can Download code here and you can put that in your active theme’s functions.php file.
I am going to use init hook.
add_action('init','my_date_archive_for_category');
We can check if incoming request contains /archives/category/ inside my_date_archive_for_category function. If it does, this is our category/date archive page.
Now breakup incoming request, and check every part of the request. We need category slug, year and month in place.
$request_parts = explode('/', $request);
Parts of request
Array ( [0] => [1] => archives [2] => category [3] => category-slug [4] => yyyy [5] => mm [6] => )
Here, we need to validate a few parts of the request like category slug, yyyy and mm.
$category_array = get_term_by('slug', $request_parts["3"], "category", ARRAY_A);
Now time to query wordpress.
query_posts(array('cat'=>$category_array["term_id"], 'year'=>$request_parts["4"], 'monthnum'=>$request_parts["5"]));
If we have posts, display them otherwise set a flag which we can use later to show 404 so visitors do not get confused.
if ( have_posts() ) : while ( have_posts() ) : the_post();
........
endwhile;
else:
$is404 = true; /* set the flag so we know no posts found and display 404. */
endif;
DO NOT FORGET to put
exit;
at the end.
Putting exit at the end will end further PHP execution on your blog.
Do not forget to let me know how this works on your blog.
June 24th, 2009 at 6:35 am
So if I download this code where do I put it?
You can put that in your active theme’s functions.php
September 2nd, 2009 at 6:13 am
I don’t understand how this can work, when I try to use the archives URL I get the Apache 404 page, it’s not even getting into Wordpress for the init hook to work.
September 2nd, 2009 at 6:39 am
OK I see now you have to have friendly permalinks enabled so that mod_rewrite redirects the request to index.php
I added some code to fix $request_parts if your blog is installed in a subdirectory:
while($request_parts[1] != “archives”) {
array_shift($request_parts);
}
September 10th, 2009 at 11:24 am
There is anyway that I can put a unique title?
Thanks
@Alex, I quite do not understand.
September 10th, 2009 at 4:52 pm
Well, I have tested your great snippet.
But, when access /archives/category/{category-slug}/yyyy/mm/ there are no title besides blog title. There is any way that I can add a unique title?
Thanks.
You can try combination of init hook and wp_head. use init hook to check you are on /archives/category/{category-slug}/yyyy/mm/ page and if you are, use wp_title hook.
Reference: http://codex.wordpress.org/Template_Tags/wp_title
September 11th, 2009 at 5:14 am
That doesn’t help, because the template of that url is under functions.php and I do not have access to tag.
@Alex, then you will need to create a php template that has everything you want and include that template inside
if ( have_posts() ) : while ( have_posts() ) : the_post();
//include new template here
endwhile;