Skip to main content
Commonmark migration
Source Link

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 -

#Update

Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

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 -

Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

Correct error where $template was not correctly returned
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38

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 $new_template;$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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

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( $page_template . '.php' ) );
    }

    return $new_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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

Amendment to code to include forgotten line
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38

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( $page_template . '.php' ) );
    }

    return $new_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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

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( $page_template . '.php' ) );
    }

    return $new_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 -

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( $page_template . '.php' ) );
    }

    return $new_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 -

#Update

In my original answer I forgot to include global $post;. I've now amended the answer to include this line.

added 33 characters in body
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38
Loading
Source Link
David Gard
  • 3.3k
  • 5
  • 26
  • 38
Loading