Change Theme Template in WordPress

If you need to change the template for WordPress based on the category of the post/page, on whether or not the current post is a page or a post, etc…, then you want to use the action hook template_redirect. For example, you may want to use a variation of the following code:

add_action('template_redirect', 'your_redirect_function');

function your_redirect_function() {
    $template_path = TEMPLATEPATH . '/' . "yourtemplatename.php";
    if(file_exists($template_path)){
        include($template_path);
        exit;
    }
}

The exit is used to exit the action hook and to stop the loading of the default template. You can check categories, titles of pages, etc… within the function to determine whether or not to use the alternate template or the default template.

Leave Comment