Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a action/filter to replace replace a category archive page with the contents of a page. I'm thinking of testing the category ID, and replacing the WP_Query object with a new one tailored for the page I want to grab, thus avoiding to have to modify the template.

Which filter/action could I use, and is there perhaps a better solution?

share|improve this question

1 Answer 1

You're looking for template_redirect:

add_action('template_redirect', 'my_category_template');

function my_category_template(){
  if(is_category(5)){ // <- category ID/title/slug
     require "path/to/your/template.php";
  }
}

If you only need to change the query arguments, then see Mike's answer to this question

share|improve this answer
    
I don't want to reuse a template, but to change the query parameters so that WordPress doesn't attempt to load the category archives, but the contents of a page. –  Andrei Sep 18 '11 at 13:35
    
See the linked question, check for $query->is_category() and add $query->set('p', $your_page_id); –  onetrickpony Sep 18 '11 at 13:56
    
My bad, I hadn't read that line. I've had some success with overriding $wp_query by returning a new WP_Query object with the query I want in that function, however the template doesn't pick it up. How could I access that in the template and isn't the loop supposed to pick it up automatically? If I do a global $wp_query ; var_dump($wp_query) in the template page it's not the same object the function is returning, if I var_dump() the object in the function context it seems to work fine, ie. it's the correct object. –  Andrei Sep 18 '11 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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