Given the following code in a WordPress theme file:
get_template_part('content', 'single');Assuming the theme folder contains content-single.php and content.php, which file will be loaded and displayed?
get_template_part('content', 'single');
Think about how WordPress looks for template parts when a slug and name are provided.
WordPress first looks for a file named {slug}-{name}.php. If it exists, it loads that file. Otherwise, it falls back to {slug}.php.
You want to include a template part template-parts/header.php and pass a variable $title to it. Which code snippet correctly does this?
Check the WordPress 5.5+ feature for passing variables to template parts.
Since WordPress 5.5, get_template_part accepts a third argument as an array of variables to pass to the template part.
Consider this code in a WordPress theme:
get_template_part('sidebar', 'footer');The theme folder contains sidebar-footer.php but the file is not loaded. What is the most likely cause?
get_template_part('sidebar', 'footer');
Check the file name and location carefully.
get_template_part looks for {slug}-{name}.php in the theme root or child theme root. If the file is missing or named differently, it won't load.
What is the output or behavior when this code runs in a WordPress theme?
get_template_part('nonexistent-part');Assume no file named nonexistent-part.php exists in the theme.
get_template_part('nonexistent-part');Think about how WordPress handles missing template parts silently.
get_template_part tries to include the file. If it doesn't exist, it fails silently without error or output.
In header.php you have:
$color = 'blue';
get_template_part('partials/colors', null, ['color' => 'red']);
echo $color;In partials/colors.php you have:
echo $color; $color = 'green';
What is the final output of echo $color; in header.php after the call?
$color = 'blue'; get_template_part('partials/colors', null, ['color' => 'red']); echo $color;
Consider variable scope when passing variables to template parts.
Variables passed via the third argument are extracted only inside the template part file scope. Changes inside the template part do not affect the original variable in the calling file.