Organise WordPress Theme Templates with get_template_part() and Partials

Organise theme templates with get_template_part()

When a theme grows, the same markup appears in many templates: the post card in the archive, the search results and the related posts, the hero section on several page templates. Copies are quick at first, and then a design change means editing six files. get_template_part() loads a partial template by name, and a child theme can replace any partial without touching the rest.

Problem: The post card is copied into archive.php, search.php and index.php, and every change has to be made three times.

Solution: Move the markup into template-parts/content.php (and variants like content-page.php) and load it with get_template_part( 'template-parts/content', $variant ). Pass values to the partial with set_query_var() or by including it with locate_template().

Naming and variants

theme/
├── template-parts/
│   ├── content.php          get_template_part( 'template-parts/content' )
│   ├── content-page.php     get_template_part( 'template-parts/content', 'page' )
│   ├── content-book.php     get_template_part( 'template-parts/content', 'book' )
│   └── content-none.php     get_template_part( 'template-parts/content', 'none' )

With a second argument WordPress first looks for content-{name}.php and falls back to content.php, in the child theme first and then in the parent. That makes the post type a natural variant:

<?php
// archive.php
while ( have_posts() ) {
    the_post();
    get_template_part( 'template-parts/content', get_post_type() );
}

if ( ! have_posts() ) {
    get_template_part( 'template-parts/content', 'none' );
}

Inside the partial all template tags work, because the loop has already set up the current post.

The partial

<?php
// template-parts/content.php
$image_size = get_query_var( 'ha_image_size' ) ? get_query_var( 'ha_image_size' ) : 'medium';
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'card' ); ?>>
    <?php if ( has_post_thumbnail() ) : ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( $image_size ); ?></a>
    <?php endif; ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
</article>

Passing values

A partial is loaded inside a function, so variables of the calling template are not visible in it. Two ways to pass data:

<?php
// 1. A query var: readable in the partial with get_query_var()
set_query_var( 'ha_image_size', 'large' );
get_template_part( 'template-parts/content', get_post_type() );
set_query_var( 'ha_image_size', '' ); // reset, the value would stay for later partials

// 2. Include the located file yourself: the partial sees the variables of this scope
$ha_card = array( 'image_size' => 'large', 'show_excerpt' => false );
include locate_template( 'template-parts/card.php' );

The second way keeps the child theme override (locate_template() looks in the child theme first) but skips the get_template_part_{$slug} hook. For page layouts that the editor chooses, see custom page templates.

Update (2020): WordPress 5.5 added a third parameter: get_template_part( 'template-parts/content', 'book', array( 'image_size' => 'large' ) ), available in the partial as $args. On 5.5+ this replaces both workarounds above.

NOTE: A child theme overrides a partial by having a file with the same path, for example child-theme/template-parts/content-book.php. This works only for files loaded with get_template_part() or locate_template(); a parent theme that uses require get_template_directory() . '/...' cannot be overridden this way.