For further reading
For further information on template parts, visit https://developer.wordpress.org/reference/functions/get_template_part/ in the WordPress Codex
Template parts are reusable code blocks that can be utilised across multiple templates or pages. Their main purpose is to simplify the development process by allowing you to create code snippets that can be used in multiple areas of a theme.
Template parts can contain any code that is required to display specific content. For example, a header, footer, sidebar, or a specific post format. WordPress provides several built-in template parts that can be used as a starting point for custom development. These include:
header.php
orheader-{name}.php
– called withget_header()
orget_header('{name}')
footer.php
orfooter-{name}.php
– called withget_footer()
orget_footer('{name}')
sidebar.php
orsidebar-{name}.php
– called withget_sidebar()
orget_sidebar(’{name}’)
comments.php
You can also create custom template parts, which you can include in your required pages. Project Zero uses some template parts in the index.php
and archive.php
files, with the get_template_part()
function. You can see this on line 41
in the index.php
file.
get_template_part( 'inc/parts/post', 'archive' );
The same line can be found on line 71
in archive.php
. The function looks inside the /inc/parts/
folder of the theme
Using template parts in a WordPress theme results in a more organised and easier-to-maintain codebase. It also allows for more efficient development, as developers can reuse code snippets across different areas of a theme.