Home WordPress General Functions get_template_part() WordPress get_template_part() Function 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.
<?php
get_template_part( 'content', 'single' ); Call What 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 get_template_part( string $slug, string|null $name = null, array $args = [] ): void|false Parameters Parameter Description $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 Value Description void|false The 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.
1. Load a content template 2. Use a folder 3. Pass data to the part Example 1 — Load a content template Show each post with content-single.php.
<?php
while ( have_posts() ) {
the_post();
get_template_part( 'content', 'single' );
} Loads content-single.php
If content-single.php does not exist:
loads content.php How It Works the_post() sets up the current post.The slug is content and the name is single. WordPress loads content-single.php or falls back to content.php. Example 2 — Use a folder Keep parts in a template-parts folder.
<?php get_template_part( 'template-parts/hero' ); ?> Loads template-parts/hero.php
<section class="hero">...</section> How It Works The slug can include a folder path. WordPress looks inside your theme folder. The file prints a hero banner. Example 3 — Pass data to the part Send a setting to a card template.
<?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>';
} <time>November 12, 2026</time> How It Works The third argument is an array of values. Inside the part, the array is named $args. The card prints the date only because show_date is true. Frequently Asked Questions What does get_template_part() do? It loads a reusable theme file, such as content-single.php. It helps you avoid repeating code.
Should I add .php to the slug? No. WordPress adds .php for you.
How does the fallback work? With a slug and a name, WordPress tries slug-name.php first, then slug.php.
How do I pass data to the part? Use the third argument, an array. In the part file it is available as $args (WordPress 5.5 and later).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion