Project Zero

Template Parts

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 or header-{name}.php – called with get_header() or get_header('{name}')
  • footer.php or footer-{name}.php – called with get_footer() or get_footer('{name}')
  • sidebar.php or sidebar-{name}.php – called with get_sidebar() or get_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.

Join the conversation

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.