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.

Let's say I want to create a custom template for my taxonomy customtax.

This is pretty simple, I just create a taxonomy-customtax.php in my theme directory.

Now, I'ld like to query a built-in category with one term of my custom taxonomy, like so:

www.example.com/?customtax=x&category_name=y

The problem here is that Wordpress always uses the archive.php (or category.php I think) instead of my taxonomy-customtax.php template.

Is there a way to invert this behaviour?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can tell WordPress what template file to use with the template_redirect hook.

add_action( 'template_redirect', 'wpse98738_taxonomy_redirect' );
function wpse98738_taxonomy_redirect() {
    if( 'x' == $customtax && 'y' == $category_name ) {
        include( get_template_directory() . '/taxonomy-customtax.php' );
        // if you're using a Child Theme, use the following line instead:
        // include( get_stylesheet_directory() . '/taxonomy-customtax.php' );
        exit; // so that WP quits looking for templates to load
    }
}

Reference

Codex pages for:

template_redirect

get_template_directory() / get_stylesheet_directory()

share|improve this answer
add comment

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.