1

When we create or edit a page, we can often set a page template depending of the used theme. Let say we have a "default" and a "with sidebar" templates.

My home page is a list of the last articles, as it is with the default Wordpress settings. It's not set as a static page.

Is it possible to set the "with sidebar" template to this page?

1 Answer 1

1

The Template Hierarchy has a set of filters to let you modify what template gets loaded for each type of query. In this case you can use home_template.

You'll have to do some work to implement your own interface for choosing a template, via the Theme Mods API/Customizer API, or there are also a number of plugins to let you create theme option pages.

In this example, we hook home_template and check for an Option named wpd_home_template_shows_sidebar, and load the template with-sidebar.php if it exists. Note that we return the $template passed to the function in either case.

function wpd_home_template( $template ) {
    if( get_option( 'wpd_home_template_shows_sidebar' ) ){
        $template = locate_template( 'with-sidebar.php' );
    }
    return $template;
}
add_filter( 'home_template', 'wpd_home_template' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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