search ]

Passing Variables using get_template_part in WordPress

Similar to the standard include() or require() PHP functions, WordPress provides its own function for loading templates (PHP files), called get_template_part(). As its name suggests, it is designed to call parts of templates in WordPress sites.

While using the get_template_part() function serves the purpose in most cases, there are situations where using it can be challenging for developers.

Specifically, it doesn’t allow passing variables (PHP variables) to the template part that you call using get_template_part(). Let’s look at some basic examples and provide a solution to this issue.

Why Use get_template_part?

In one (actually two) words – child theme. If you want to modify files in your theme using a child theme, using get_template_part() comes in handy.

In essence, when you call get_template_part('some-file'), WordPress first checks whether that file (located in the same directory) is in your child theme.

If it is found, WordPress loads it from the child theme instead of the main theme, providing a convenient solution to modify the behavior of the theme without touching the source files of the main theme. Here is a basic example with a file named loop.php:

<?php 
/* loop.php file includes content.php */ 
while( have_posts() ): 
  the_post(); 
  get_template_part( 'content' ); 
endwhile;
?>

And here is a basic example of a file content.php:

<?php 
/* content.php */ 
 
the_title(); 
the_content();
 
?>

The Problem with Variables in get_template_part

But what if we have variables in the loop.php file and we want to use them within the content.php file that we call using get_template_part()? Take a look at the following example:

<?php 
/* Using a variable */
 
$example = 'This is an example variable'; 
 
while( have_posts() ): 
  the_post(); 
  get_template_part( 'content' ); 
endwhile;
 
?>

When we try to use the variable $example inside content.php, it simply won’t work.

 
<?php 
/* content.php */ 
 
the_title(); 
the_content(); 
echo $example; //Undefined 
 
?>

The Solution – Using include and locate_template Function

If you dig into the WordPress code, you’ll find that the get_template_part function uses another function called locate_template(), which is designed to identify whether the template part we are calling is in the child theme.

This function can either load the template part (and clean up all the variables using get_template_part) or simply return the path of the file without loading it.

Therefore, using locate_template() along with the PHP include() function can give us the desired result. Take a look:

 
<?php
/* Using a variable */
 
$example = 'This is an example variable'; 
 
while( have_posts() ): 
  the_post(); 
  //Note: you need to use the full file name, in this case, content.php, and also false arguments to avoid loading the file and return the full path instead 
  include( locate_template( 'content.php', false, false ) ); 
endwhile;
 
?>

Now, when we try to use the variable $example inside content.php, everything will work smoothly.

 
<?php 
/* content.php */ 
 
the_title(); 
the_content(); 
echo $example;
//The variable is recognized without the need for a global statement ;) 
 
?>

So, the title of the article is a bit misleading, but the principle is quite simple and understandable. Hope it helps, and feel free to share your thoughts… 🙂

Roee Yossef
Roee Yossef

I develop custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.

0 Comments...

Leave a Comment

Add code using the buttons below. For example, to add PHP click the PHP button & add the code inside the shortcode. Typo? Please let us know...