Skip to content

Using r_get_template_part

Jim Reevior edited this page Aug 14, 2018 · 1 revision

Using r_get_template_part()

In WordPress, there is a function called get_template_part(). This is a very powerful function that allows a theme to have tiers of templates that fall back to a default template.

One downfall of this function, though, is that if no default template is present, nothing will display. There are scenarios though, where this is not ideal.

Example: Say there is a custom post type, custom_type.

get_template_part()

<?php get_template_part( 'template-parts/custom_type', 'archive' ); ?>

These are the template part files that will be searched for in order:

  • child-theme/template-parts/custom_type-archive.php
  • parent-theme/template-parts/custom_type-archive.php
  • child-theme/template-parts/custom_type.php
  • parent-theme/template-parts/custom_type.php

If none of these files are present, the content will not be output.

r_get_template_part()

This function adds an additional fallback template, content.php. This file is present in Responsive Framework, so it guarantees that the content will always be displayed, even if specific template parts are not present in the child theme.

<?php r_get_template_part( 'template-parts/custom_type', 'archive' ); ?>

This function will search for templates in the following order:

  • child-theme/template-parts/custom_type-archive.php
  • parent-theme/template-parts/custom_type-archive.php
  • child-theme/template-parts/custom_type.php
  • parent-theme/template-parts/custom_type.php
  • child-theme/template-parts/content.php
  • parent-theme/template-parts/content.php

Where To Use

This function should only be used to display single post template parts. Sections should be loaded using get_template_part() only.

Welcome to Responsive!

Get started

Configuration

Build child themes

Sass

Javascript

PHP

Shortcodes

Templates

GitHub

Tasks

Contribute to the framework

Code Examples

BU Developer Resources

Clone this wiki locally