The WordPress index.php is a PHP file. It acts as a standard theme file to display a home page on websites and applications. Moreover, every page on a website delivers an index.php file with different customization from the database.
For instance, if you are accessing website.com, you are most probably accessing website.com/index.php.
index.php is a “catch-all” template — it is a fallback template when no appropriate template is found within the template hierarchy.
The template hierarchy above illustrates that if WordPress cannot find a specific template file, it skips right down to index.php as the theme’s standard homepage template file.
Therefore, index.php is the code used to provide the most generic template in a theme. It acts as a front controller ensuring every page of a site is displayed appropriately.
For example, if someone visits your blog address in http://website.com/blog/category/mountains/, WordPress will look to render the template file of the current page, which is category-mountains.php.
However, if the specific template file is missing, WordPress will look for a generic category template file like category.php.
If that file is also missing, WordPress will fall back to the main theme template file that is index.php.
Because of that, the index.php code should be as basic as it can get, as it needs to be reliable for every new page loaded.
Here is what a default wordpress index.php code looks like:
<?php /** * Front to the WordPress application. This file doesn't do anything, but loads * wp-blog-header.php which does and tells WordPress to load the theme. * * @package WordPress */ /** * Tells WordPress to load the WordPress theme and output it. * * @var bool */ define('WP_USE_THEMES', true); /** Loads the WordPress Environment and Template */ require('./wp-blog-header.php'); ?>
Below, I will explain how to set up a WordPress homepage and how index.php differs from front-page.php and home.php.
Setting WordPress Homepage
WordPress has two ways of displaying a site’s homepage: latest post and static page display.
By default, WordPress shows a list of the latest post in reverse chronological order at a website’s homepage.
However, users can customize and choose a static homepage for their website. This will make the site feel like a landing page. Choosing a static homepage is applicable if you want to have your blog posts on a dedicated page different from the main page.
There are several ways to customize your homepage. The first way is to create a theme template file. You can either create a front-page.php file or a home.php file.
front-page.php is used for both the latest post and static page display. However, if the template file is missing, WordPress will use the home.php template file.
If WordPress can’t find both front-page.php and home.php, it will fall back to index.php.
The second way to modify your homepage is through the Settings » Reading section in your WordPress admin panel, which allows you to start customizing.
Differences between index.php, home.php, and front-page.php
index.php, home.php, and front-page.php template files help in displaying the front page of a website. However, a lot of confusion is present when it comes to understanding the difference between the three.
- front-page.php — used in both “latest posts” and “static page” homepage setting
- home.php — used when front-page.php isn’t found in ”latest posts” as homepage. Also, WordPress will look for this file when the posts page is set as the homepage
- index.php — used when “latest posts” are set as the homepage but home.php is missing.
The order in which WordPress tries to find the home page template is front-page.php, home.php, and then index.php. If any of those files are missing, WordPress will try finding the next one in that order.
The front-page.php file will always be used on your site front page. The file will be used if a static front page is set and a visitor goes to the page. If the template file isn’t found, then index.php will be loaded.
Meanwhile, the home.php template file is used to display blog posts, regardless of whether the posts are displayed on the homepage or other pages. If the file is not present, index.php will come forward.
If both front-page.php and home.php template files exist, for example, if the blog posts are set as the homepage, then the front-page.php file will overwrite the home.php file.
To summarize, use home.php to customize your list of posts, front-page.php to customize a static home page, and keep in mind that index.php is a fallback template only — in case no appropriate template was found.
The index.php file is a fallback template file for WordPress themes. This means that the file will take care of a web page in case no appropriate template file was found.
The file is commonly mixed up with the other two front page template files: home.php and front-page.php. The first is used for blogs. While the latter is used for landing pages.Now that you have a better understanding of index.php, you can try tinkering further with the WordPress Settings » Reading section to customize your website’s front page.