WordPress get_template_part() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template loader

Definition

In WordPress, get_template_part() loads a small reusable piece of your theme, such as a post card or a hero banner. It keeps big templates short and clean.

With get_template_part( 'content', 'single' ), WordPress looks for content-single.php first. If it is missing, it falls back to content.php.

Syntax

Pass the slug and, if you like, a name.

WordPress
<?php
get_template_part( 'content', 'single' );
CallWhat it does
get_template_part( 'content' )Loads content.php.
get_template_part( 'content', 'single' )Loads content-single.php, then content.php.
get_template_part( 'template-parts/card', null, $args )Loads a file in a folder and passes data to it.

Do not add .php to the slug. WordPress adds it for you.

Full declaration
WordPress
get_template_part( string $slug, string|null $name = null, array $args = [] ): void|false

Parameters

ParameterDescription
$slugstring. The base name of the file, such as content or template-parts/card. Do not include .php.
$namestring|null. The name of a special version, for example single. Default is null.
$argsarray. Extra data for the file, available as $args. Default is an empty array.

Return Value

Return ValueDescription
void|falseThe part is loaded. It returns false if no file was found.

WordPress Examples

Three short theme examples. Each one shows the code and what it prints.

Example 1 — Load a content template

Show each post with content-single.php.

WordPress
<?php
while ( have_posts() ) {
  the_post();
  get_template_part( 'content', 'single' );
}

How It Works

  1. the_post() sets up the current post.
  2. The slug is content and the name is single.
  3. WordPress loads content-single.php or falls back to content.php.

Example 2 — Use a folder

Keep parts in a template-parts folder.

WordPress
<?php get_template_part( 'template-parts/hero' ); ?>

How It Works

  1. The slug can include a folder path.
  2. WordPress looks inside your theme folder.
  3. The file prints a hero banner.

Example 3 — Pass data to the part

Send a setting to a card template.

WordPress
<?php
get_template_part( 'template-parts/card', null, array(
  'show_date' => true,
) );

// in template-parts/card.php
if ( ! empty( $args['show_date'] ) ) {
  echo '<time>' . get_the_date() . '</time>';
}

How It Works

  1. The third argument is an array of values.
  2. Inside the part, the array is named $args.
  3. The card prints the date only because show_date is true.

Frequently Asked Questions

It loads a reusable theme file, such as content-single.php. It helps you avoid repeating code.
No. WordPress adds .php for you.
With a slug and a name, WordPress tries slug-name.php first, then slug.php.
Use the third argument, an array. In the part file it is available as $args (WordPress 5.5 and later).

Next: post_type_archive_title()

Print or return the title of a custom post type archive.

post_type_archive_title() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful