How to override template files in WordPress
Template files are the files which control how your WordPress site will be displayed on the web. These files draw information from your WordPress database and generate the HTML code which is sent to the web browser.
Every theme has several different template files, where every template has a specific task. Some templates (the header and footer template files for example) are used on all the web pages, while others are used only under specific conditions.
Important Template Files:
- header.php (displays the header area)
- footer.php (displays the footer area)
- index.php (shows your latest blog posts)
- single.php (shows a single blog post)
- page.php (displays static page)
- sidebar.php (displays the sidebar)
- archive.php (shows blog archives)
Please see also the Template Hierarchy Codex Page.
How to override template files
In case you want to customize the HTML of a certain area of your WordPress theme, you can override template files with a child theme. This WordPress feature lets you modify the templates of a parent theme without actually editing them, so that your modifications are preserved when the parent theme is updated. How to create a child theme? has been explained in the previous article.
A child theme can override any parental template by simply using a file with the same name. That means when you want to override a template file, you can copy the original file from the parent theme and paste it in the child theme folder with the same file name. WordPress will then use the file of the child theme instead of the parent template. Afterwards you can edit the file in your child theme folder and make your changes.
Example:
- let’s say you want to edit the page.php
- open the page.php file and save a copy in the child theme
- edit and save the page.php in the child theme