6

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.

2
  • I think the first option is better. I don't think making any plugin for that is necessary as it's already done for you in Wordpress core.
    – sakibmoon
    Commented Aug 16, 2014 at 3:32
  • @sakibmoon I would normally agree with you, but in my use case (a large site that is worked on by many people), the suggestion in my accepted answer is one that is going to have the most ease of use and cause the least confusion. Commented Aug 16, 2014 at 20:12

2 Answers 2

8

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.

1
  • Thank you so much for your suggestion. The get_template_part option is what I'm looking for here. I actually had never heard of the get_template_part hook until now. I have created something simple in my functions.php that allows the selection of one of the layouts. Commented Aug 16, 2014 at 20:11
1

There is only one option, the page templates one. This is how 99% of the themes work and what wordpress users and implementators are used to. You can try to be unique but no one will appreciate it.

1
  • Thanks for your answer. In this case (a large site that is worked on by many people) I think the unique thing will be appreciated actually. I can create a clearer and easily understood UI for doing so with the suggestion in my accepted answer. Commented Aug 16, 2014 at 20:13

Your Answer

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

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