I essentially want to create several different front-page.php templates with different loops and layouts and then present an option in the admin panel to select one template on one day and a different template on another day.
This can be done, I suppose, by fooling WordPress and selecting a static page to appear as the front page and selecting a custom page template from the page edit screen which really is not a “static” page, but instead a page with a full loop – one of the several custom page templates that would have been my front-page.php options.
Another way would be to have all my loops and custom layouts enclosed in if statements in the one front-page.php and presenting options that engage one of the if statements each time. (This seems reasonable – though a bit clumsy to me – but I have a feeling it’s not so great and that there are problems, like load times increasing.)
Are there any options I’m missing? Or perhaps plugins which do this for me? I must admit I’m surprised that I haven’t been able to find a plugin which allows you to select from a list of templates to display as the front-page.php/index.php page without having to resort to the static page hack – variations of loops and homepage layouts must be a common request.
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.