I'd recommend using the template_include
filter as opposed to template_redirect
, as using this hook means that you don't redirect the user but rather just present them with the template that is requierd.
add_filter('template_include', 'my_custom_template_redirect', 99);
function my_custom_template_redirect($template){
global $post;
if(is_single() && 'campaign' == get_post_type($post)){
$page_template = get_page_template();
$new_template = locate_template( array( 'subfolder/' . $page_template . '.php' ) );
$template = ($new_template !== '') ? $new_template : $template;
}
return $template;
}
Ovbiously I'm making the assumption that your $page_template
variable doesn't have .php
on the end, but you can ammend that as required.
I'd recommend you have a read of the documentation related to this hook, and the locate_template
function -
template_include
filter hook - http://codex.wordpress.org/Plugin_API/Filter_Reference/template_includelocate_template()
- http://codex.wordpress.org/Function_Reference/locate_template
#Update
Update
In my original answer I forgot to include global $post;
. I've now amended the answer to include this line.