0
0
Wordpressframework~20 mins

Template parts in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Parts Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this WordPress template part usage?

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?

Wordpress
get_template_part('content', 'single');
ABoth content-single.php and content.php are loaded and displayed.
BThe file content.php is loaded and displayed.
CThe file content-single.php is loaded and displayed.
DNo file is loaded because get_template_part requires a full filename.
Attempts:
2 left
💡 Hint

Think about how WordPress looks for template parts when a slug and name are provided.

📝 Syntax
intermediate
2:00remaining
Which option correctly includes a template part with a variable passed in WordPress?

You want to include a template part template-parts/header.php and pass a variable $title to it. Which code snippet correctly does this?

A
use get_template_part with third argument:
<?php get_template_part('template-parts/header', null, ['title' => 'Welcome']); ?>
B
pass variable directly as second argument:
<?php get_template_part('template-parts/header', ['title' => 'Welcome']); ?>
C
use include with variable:
<?php include 'template-parts/header.php'; ?>
D
set query variable and then call get_template_part:
<?php set_query_var('title', 'Welcome'); get_template_part('template-parts/header'); ?>
Attempts:
2 left
💡 Hint

Check the WordPress 5.5+ feature for passing variables to template parts.

🔧 Debug
advanced
2:00remaining
Why does this template part not load the expected file?

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?

Wordpress
get_template_part('sidebar', 'footer');
AThe file sidebar-footer.php is missing or misnamed in the theme folder.
BThe function get_template_part does not accept two arguments.
CThe file sidebar-footer.php must be in a subfolder named 'template-parts'.
DThe second argument must be null to load sidebar-footer.php.
Attempts:
2 left
💡 Hint

Check the file name and location carefully.

component_behavior
advanced
2:00remaining
What happens if get_template_part is called with a non-existent template part?

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.

Wordpress
get_template_part('nonexistent-part');
AWordPress throws a fatal error and stops loading the page.
BA warning message is displayed on the page about missing template.
CWordPress loads the default index.php template instead.
DNothing is output and the page continues loading normally.
Attempts:
2 left
💡 Hint

Think about how WordPress handles missing template parts silently.

state_output
expert
2:00remaining
What is the value of $color after this template part call?

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?

Wordpress
$color = 'blue';
get_template_part('partials/colors', null, ['color' => 'red']);
echo $color;
Ared
Bblue
Cgreen
DNotice: Undefined variable: color
Attempts:
2 left
💡 Hint

Consider variable scope when passing variables to template parts.