I’m really confused with index.php
, home.php
and front-page.php
. In many cases even though I had an index.php
, I overwrite it with front-page.php
. But recently, I became aware about home.php
.
- What’s the difference between the
home.php
andindex.php
? - What’s the ideal condition to use a
home.php
than anindex.php
? - What’s the ideal condition to use a
front-page.php
? - When I’m using a
front-page.php
then what specific task anindex.php
is doing for me then?
I’ve read the Template Hierarchy in Codex. Please don’t mix my question with Page Templates, I understand ’em, alHamduLILLAH.
Front page logic is one of the most confusing features in WordPress and is exceptionally hard to explain and summarize. As mentioned in comment while back I burnt unholy amount of time to put together my front page logic cheat sheet for it.
But since this is a popular thread let me try to answer those very specific questions you had.
home.php
is template for posts index (archive of native Post post type, which is a special case in WP). WP will attempt to look it up for index of posts, whether they are displayed at the root of the site or at dedicated posts page.index.php
is catchâall template. It is final choices in all branches of template hierarchy and will be picked when nothing else fits, for both archives and singular views.Only posts index can use
home.php
, but all other contexts might and will useindex.php
.You use
home.php
to customize posts index.You use
index.php
to provide the most generic template in your theme, suitable for display of anything.Some themes choose to have empty
index.php
and ensure they have more specific templates for all possible cases, so it never has to be used.front-page.php
is used for posts index at the root or static front page, if enabled.It is a high priority template, so if theme has it you cannot select arbitrary template for static front page. For this reason it is almost never included in publicly released themes (which is correct).
The best use for it is in private projects, since it’s easier to configure than page template.
index.php
is still a catch all template for all other cases.If you use static front page (to which
front-page.php
will apply) then your posts page will try to usehome.php
and thenindex.php
.The
front-page.php
file is the site front page template. It will always be used on your site front page, regardless of whetherget_option( 'show_on_front' )
is set topage
orposts
.The
home.php
template file is the blog posts index template. It will always be used to display your blog posts index, regardless of whether the blog posts index is displayed on the site front page, or on a different page.In the case that both
front-page.php
andhome.hpp
exist, and theget_option( 'show_on_front' )
is set toposts
(i.e., the site front page displays the blog posts index), thefront-page.php
file will take precedence over thehome.php
file.The
index.php
file is the default fallback template for all contexts in the Template Hierarchy. It is only ever used if a more-specific template file does not exist for the current context.The template hierarchy for the site front page is:
front-page.php
'page' == get_option( 'show_on_front' )
: page hierarchy'posts' == get_option( 'show_on_front' )
: blog posts index hierarchyThe template hierarchy for the blog posts index is:
home.php
index.php
The template hierarchy for pages is:
page-{slug}.php
page-{id}.php
page.php
index.php
All this comes from a thorough reading of the Template Hierarchy.
home.php
is used if:http://example.com/blog/
)home.php
fileIf the last condition isn’t met — ie, there is no
home.php
file in the theme — thenindex.php
will be loaded.front-page.php
will be used if:http://example.com/
), andfront-page.php
fileIf the last condition isn’t met — ie, there is no
front-page.php
file in the theme — thenindex.php
will be loaded.In summary
If you want to customize your list of posts, use
home.php
.If you want to customize your static home page, use
front-page.php
.In short:
index.php
is a fallback template only, in case no appropriate template was foundhome.php
is used for the blog (a listing of recent posts)front-page.php
is used for the landing-pageThe universal
index.php
templateThe
index.php
template file is a fallback template. It is used as a last resort when no other more appropriate template is available. For example, if you don’t have afront-page.php
nor ahome.php
thenindex.php
will be used. This is also true for missingarchive.php
orsingle.php
and so on.Regarding
front-page.php
VShome.php
Now the difference between
front-page.php
andhome.php
templates is that thefront-page.php
is used, as its name suggests, as the main front-page of the site, whilehome.php
is intended to be the home of the Blog section.What template will be used for my site’s landing-page?
The
front-page.php
template, if present, will be used for the site’s main front page (e.g.http://www.example.com/
). If not present, thenhome.php
will be used instead. If both thefront-page.php
andhome.php
template files are missing then theindex.php
fallback template will be used.What template will be used for my blog page?
WordPress allows you to have a “Blog” page (which will list recent posts) on a different page than the landing-page (e.g.
http://www.example.com/blog/
). So if this is the case on your site, then the Blog page will always use thehome.php
template. Ifhome.php
doesn’t exist then theindex.php
fallback template will be used.How to configure the landing-page and blog page?
To configure what content to show on your front page, go to the WordPress Admin, under Tools > Reading, there you can configure the “Front page displays” to show a static page, or your latest posts.
In the case you choose to display a static page on the front page, then you also have the possibility to set which page to use as a placeholder for the Blog page (which will use the
home.php
template):taken from WordPress template hierarchy
Template hierarchy chart:
I decided to test which template is used for each of the following pages when the various Front page displays options are set.
The results are quite long, but can be used as a reference or cheat-sheet.
Source: How do the front-page.php and home.php templates differ in WordPress?
Note to editor: I tried to copy and paste the tables here but markdown format is required instead of HTML. Please convert to markdown if you can.