{"id":9623,"date":"2021-02-16T02:05:20","date_gmt":"2021-02-16T07:05:20","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=9623"},"modified":"2021-11-30T09:19:06","modified_gmt":"2021-11-30T09:19:06","slug":"pass-variables-via-get_template_part-wordpress","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/","title":{"rendered":"How to Pass Variables via get_template_part in WordPress"},"content":{"rendered":"\n
As a WordPress<\/a> theme developer, sooner or later, you will encounter a situation where you need to declare a variable in one file and use it in another included one and find that you cannot directly do it. The good news is that you can pass variables via get_template_part<\/em> in WordPress using two methods that we will be highlighting here.<\/p>\n\n\n\n As of WordPress 5.5, passing variables via get_template_part<\/em> is now part of the core. Previously you could only pass two parameters, slug, and name. Starting in WordPress 5.5, the template loading functions allow additional arguments to be passed through to the matched template file using a new $args<\/em> parameter as the third parameter.<\/p>\n\n\n\n This is the syntax:<\/p>\n\n\n\n <\/p>\n\n\n\n Here is an example of how to use the WordPress get_template_part<\/em> function:<\/p>\n\n\n\n <\/p>\n\n\n\n In the included template file (i.e. template-parts\/geometrical-entities<\/strong>), you can gain access to the variables as show below:<\/p>\n\n\n\n <\/p>\n\n\n\n Alternately, for cleaner code, you can set up defaults first, using wp_parse_args<\/em>:<\/p>\n\n\n\n <\/p>\n\n\n\n This option is good if you have older versions of WordPress. It is used to set the value of a query variable in the WP_Query<\/em> class with the following syntax.<\/p>\n\n\n\n <\/p>\n\n\n\n With this you set the variable in your theme file as follows:<\/p>\n\n\n\n <\/p>\n\n\n\n In your template-part\/team<\/strong> file you can retrieve the variable like so:<\/p>\n\n\n\n <\/p>\n\n\n\n Note:<\/strong> The variable set by set_query_var<\/em> stays in the globals. You will need to:<\/p>\n\n\n\n <\/p>\n\n\n\n After the get_template_part<\/em> call otherwise it will be available in the next call of the template.<\/p>\n\n\n\n These two methods are the proper ways to variables via get_template_part<\/em> in WordPress. The choice of which one to use is a matter of preference as well as the oldest version of WordPress that you intend to support as a plugin or theme developer.<\/p>\n\n\n\n Ref: [ 1<\/a> ],[ 2<\/a> ]<\/p>\n","protected":false},"excerpt":{"rendered":" As a WordPress theme developer, sooner or later, you will encounter a situation where you need to declare a variable in one file and use it in another included one…<\/p>\n","protected":false},"author":1,"featured_media":9624,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,27,16],"tags":[136,164,303,424,452,636,638,651],"yoast_head":"\nOption 1: Passing Variables via get_template_part<\/em> Using WordPress 5.5+<\/h2>\n\n\n\n
get_template_part( string $slug, string $name = null, array $args = null );<\/code><\/pre>\n\n\n\n
get_template_part( 'template-parts\/geometrical-entities', null, array( \n 'class' => 'shape-box',\n 'data' => array(\n 'component_type' => 'shape',\n 'component_name' => 'rectangle',\n )) \n);<\/code><\/pre>\n\n\n\n
<div class="widget <?php if ( $args['class'] ) { echo esc_html( $args['class'] ); } ?>">\n <?php if ( $args['data'] ) { echo esc_html( $args['data']['component_name'] ); } ?>\n<\/div><\/code><\/pre>\n\n\n\n
\/\/ Setup defaults\n $array_defaults = array( \n 'class' => 'featured',\n 'data' => array(\n 'component_type' => 'shape',\n 'component_name' => 'circle',\n )\n ); \n\n $args = wp_parse_args( $args, $array_defaults );<\/code><\/pre>\n\n\n\n
Option 2: Using set_query_vars<\/em><\/h2>\n\n\n\n
set_query_var( string $key, mixed $value );<\/code><\/pre>\n\n\n\n
set_query_var( 'class_name', 'team-profile' );\nget_template_part( 'template-parts\/team' );<\/code><\/pre>\n\n\n\n
<?php\n $myVariable = get_query_var( 'class_name' );\n\n if ( $myVariable ) {\n?>\n\n<div class="widget <?php echo esc_html( $myVariable ); ?>">\n ... some html here\n<\/div>\n\n<?php\n }\n?><\/code><\/pre>\n\n\n\n
set_query_var('class_name', false); <\/code><\/pre>\n\n\n\n
In Closing<\/h2>\n\n\n\n