Is it possible to set a page template on a dynamic home page (articles list)?

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' );

Leave a Comment