0

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?

1 Answer 1

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

3
  • 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
    Commented Sep 18, 2011 at 13:35
  • See the linked question, check for $query->is_category() and add $query->set('p', $your_page_id); Commented Sep 18, 2011 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
    Commented Sep 18, 2011 at 15:07

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.