Template Hierarchy: confused with index.php, front-page.php, home.php

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 and index.php?
  • What’s the ideal condition to use a home.php than an index.php?
  • What’s the ideal condition to use a front-page.php?
  • When I’m using a front-page.php then what specific task an index.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.

Related posts

6 comments

  1. 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.

    What’s the difference between the home.php and index.php?

    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 use index.php.

    What’s the ideal condition to use a home.php than an index.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.

    What’s the ideal condition to use a front-page.php?

    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.

    When I’m using a front-page.php then what specific task an index.php is doing for me then?

    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 use home.php and then index.php.

  2. The front-page.php file is the site front page template. It will always be used on your site front page, regardless of whether get_option( 'show_on_front' ) is set to page or posts.

    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 and home.hpp exist, and the get_option( 'show_on_front' ) is set to posts (i.e., the site front page displays the blog posts index), the front-page.php file will take precedence over the home.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
    • If 'page' == get_option( 'show_on_front' ): page hierarchy
    • If 'posts' == get_option( 'show_on_front' ): blog posts index hierarchy

    The template hierarchy for the blog posts index is:

    • home.php
    • index.php

    The template hierarchy for pages is:

    • Custom page template
    • page-{slug}.php
    • page-{id}.php
    • page.php
    • index.php
  3. All this comes from a thorough reading of the Template Hierarchy.

    home.php is used if:

    • Your site’s front page is set to display a list of posts, or
      • A static front page is set, and
      • A visitor goes to the page that you’ve defined as your “list of posts” (eg, http://example.com/blog/)
    • And the theme has a home.php file

    If the last condition isn’t met — ie, there is no home.php file in the theme — then index.php will be loaded.

    front-page.php will be used if:

    • A static front page is set, and
    • A visitor goes to the page that you’ve defined as your “static home page” (ie, http://example.com/), and
    • The theme has a front-page.php file

    If the last condition isn’t met — ie, there is no front-page.php file in the theme — then index.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.

  4. In short:

    • index.php is a fallback template only, in case no appropriate template was found
    • home.php is used for the blog (a listing of recent posts)
    • front-page.php is used for the landing-page

    The universal index.php template

    The 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 a front-page.php nor a home.php then index.php will be used. This is also true for missing archive.php or single.php and so on.

    Regarding front-page.php VS home.php

    Now the difference between front-page.php and home.php templates is that the front-page.php is used, as its name suggests, as the main front-page of the site, while home.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, then home.php will be used instead. If both the front-page.php and home.php template files are missing then the index.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 the home.php template. If home.php doesn’t exist then the index.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):

    enter image description here

  5. taken from WordPress template hierarchy

    Home Page display

    By default, WordPress sets your site’s home page to display your
    latest blog posts. This page is called the blog posts index. You can
    also set your blog posts to display on a separate static page. The
    template file home.php is used to render the blog posts index, whether
    it is being used as the front page or on separate static page. If
    home.php does not exist, WordPress will use index.php.

    1. home.php
    2. index.php

    Note: If front-page.php exists, it will override the home.php template.

    Front Page display

    The front-page.php template file is used to render your site’s front
    page, whether the front page displays the blog posts index (mentioned
    above) or a static page. The front page template takes precedence over
    the blog posts index (home.php) template. If the front-page.php file
    does not exist, WordPress will either use the home.php or page.php
    files depending on the setup in Settings → Reading. If neither of
    those files exist, it will use the index.php file.

    1. front-page.php – Used for both “your latest posts” or “a static page”
    as set in the front page displays section of Settings → Reading.

    2. home.php – If WordPress cannot find front-page.php and “your latest
    posts” is set in the front page displays section, it will look for
    home.php. Additionally, WordPress will look for this file when the
    posts page is set in the front page displays section.

    3. page.php – When
    “front page” is set in the front page displays section.

    4. index.php –
    When “your latest posts” is set in the front page displays section but
    home.php does not exist or when front page is set but page.php does
    not exist.

    As you can see, there are a lot of rules to what path
    WordPress takes. Using the chart above is the best way to determine
    what WordPress will display.

    Template hierarchy chart:
    Wordpress template hierarchy

  6. I decided to test which template is used for each of the following pages when the various Front page displays options are set.

    • example.net
    • example.net/home/
    • example.net/blog/

    The results are quite long, but can be used as a reference or cheat-sheet.

    Front page displays

    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.

Comments are closed.