Ways to have multiple front-page.php templates that can be swapped out?

One way is to have a single front-page.php and then using get_template_part(), to show different content based on user choice.

Rough code:

get_header();

$layout = get_option( 'front_page_layout', 'default' );
get_template_part( 'front-page', $layout );

get_footer();

After that you need to create a file for every layout, they should be called, something like:

  • front-page-one.php
  • front-page-two.php
  • front-page-three.php
  • front-page-default.php

Last one will be used when the option is not set, e.g. when the the theme is just activated.

Of course, you need a page in backend where users can choose the template and save the ‘front_page_layout’ option.

Having only one front-page.php it will be easily recognized by anyone seeing your theme without having to look at code.

Another alternative is just to use front-page.php to contain the default layout, after that, you can create some alternative layouts using page templates.

/*
Template Name: Home Page Alternative One
*/

In this way your users can create a page, assign to it one of the templates you have created and finally set this page as the front page in the reading settings.

After first time, to change home page layout users need only to change the page template of the page choose as static front page.

This second option require a bit more work for the users, but prevent you to have a theme settings page where to set the ‘front_page_layout’ option.

Leave a Comment