One section of my website is going to be completely different visually than the rest of the pages, is there a way to apply a WP Theme to specific page templates ?


In fact there is, using template_redirect, which you would put in the functions.php file - Here's what I use:

function uniquename_default_template() {
    if(get_post_type() == 'posttype') : /* You could use is_single() instead of get_post_type() == '' or any type of conditional tag) */
    include(TEMPLATEDIR . 'path/to/theme/file.php'); /* You could use TEMPLATEDIR to get a file from a template folder, or PLUGINDIR to get a file from the plugins directory - doesn't support HTTP requests */
    exit; endif;
add_action('template_redirect', 'uniquename_default_template');

The above code checks for a post_type called posttype, if it is, it will include the PHP file specified in the include statement by redirecting the template using the template_redirect function.

  • Ok so I put this function into the functions.php of my main theme. Then I have a second theme that I want to apply only to a specific page template. I can test this condition on the first line of your function with if(is_page_template('template_special_area.php') ). Then I have to include this said template, how do I do that? Doesn't TEMPLATEDIR point to the main theme fodler? – mike23 Mar 7 '11 at 9:46
  • 1
    @mike23 - that's just example code, jared suggested to have some template inside a plugin but you can naturally use some template from within another theme and then use TEMPLATEDIR to point to the right file. – hakre Mar 7 '11 at 11:22
  • @mike23 - hakre is right, you would need to use TEMPLATEDIR (which points to /public_html/wp-content/themes/) if the file is in your themes folder. But my code may be a little buggy, I haven't tested it much but it might just work for what you need. Either way, the function you're looking for to use is template_redirect - I wouldn't count on my code above to work 100%. – Jared Mar 7 '11 at 11:31
  • @mike23 - I revised my code a bit, it is in the proper format now and above is how it should be used. – Jared Mar 8 '11 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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