1

My archive.php template is formatted perfectly to list out posts from a certain month and year. However, I only want this template to show from a specific category. I can query for the specific category but then the month/year filter no longer works. How can I display posts on the archive.php template from one category?

1 Answer 1

1
add_action( 'parse_query', 'wpse_60354_archive_category' );
function wpse_60354_archive_category( $wp_query )
{
    if ( $wp_query->is_archive() )
        $wp_query->set( 'cat', ID_OF_CATEGORY );
}

Just replace ID_OF_CATEGORY with the ID of your category, obviously ;)

7
  • I was working with this and can't seem to get it to work. Where exactly would this code be dropped in?
    – Robbie
    Commented Aug 1, 2012 at 13:05
  • functions.php - it's not tested, so if you still get problems, let us know what's going on & we can tweak it! Commented Aug 1, 2012 at 13:07
  • Cool. I did try it out and it had no impact on the page. Would this go before or after the loop? I tried both.
    – Robbie
    Commented Aug 1, 2012 at 14:08
  • The code I posted needs to go in functions.php - it has to run before the template is loaded! Commented Aug 1, 2012 at 14:14
  • Ahh, I put that in my functions.php template and removed it from archive.php. I replaced the ID of category which is '3' but it is not sorting my archives page by that category.
    – Robbie
    Commented Aug 1, 2012 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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