{"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

Option 1: Passing Variables via get_template_part<\/em> Using WordPress 5.5+<\/h2>\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

get_template_part( string $slug, string $name = null, array $args = null );<\/code><\/pre>\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

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

<\/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

<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

<\/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

  \/\/ 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

<\/p>\n\n\n\n

Option 2: Using set_query_vars<\/em><\/h2>\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

set_query_var( string $key, mixed $value );<\/code><\/pre>\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

set_query_var( 'class_name', 'team-profile' );\nget_template_part( 'template-parts\/team' );<\/code><\/pre>\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

<?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

<\/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

set_query_var('class_name', false); <\/code><\/pre>\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

In Closing<\/h2>\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":"\nHow to Pass Variables via get_template_part in WordPress<\/title>\n<meta name=\"description\" content=\"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Pass Variables via get_template_part in WordPress\" \/>\n<meta property=\"og:description\" content=\"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-16T07:05:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-30T09:19:06+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to Pass Variables via get_template_part in WordPress\",\"datePublished\":\"2021-02-16T07:05:20+00:00\",\"dateModified\":\"2021-11-30T09:19:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\"},\"wordCount\":328,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg\",\"keywords\":[\"CMS\",\"Cross Platform\",\"HTML\",\"Open Source\",\"PHP\",\"Web Applications\",\"Web Development\",\"WordPress\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\",\"url\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\",\"name\":\"How to Pass Variables via get_template_part in WordPress\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg\",\"datePublished\":\"2021-02-16T07:05:20+00:00\",\"dateModified\":\"2021-11-30T09:19:06+00:00\",\"description\":\"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Pass variables via get_template_part or using set_query_vars\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Pass Variables via get_template_part in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Pass Variables via get_template_part in WordPress","description":"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"How to Pass Variables via get_template_part in WordPress","og_description":"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress","og_url":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2021-02-16T07:05:20+00:00","article_modified_time":"2021-11-30T09:19:06+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to Pass Variables via get_template_part in WordPress","datePublished":"2021-02-16T07:05:20+00:00","dateModified":"2021-11-30T09:19:06+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/"},"wordCount":328,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg","keywords":["CMS","Cross Platform","HTML","Open Source","PHP","Web Applications","Web Development","WordPress"],"articleSection":["Articles","Guides","How To","Programming","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/","url":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/","name":"How to Pass Variables via get_template_part in WordPress","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg","datePublished":"2021-02-16T07:05:20+00:00","dateModified":"2021-11-30T09:19:06+00:00","description":"This guide will show you the correct way to pass variables via get_template_part or using set_query_vars in other templates in WordPress","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/02\/variables-via-get_template_part-wordpress.jpg","width":1200,"height":630,"caption":"Pass variables via get_template_part or using set_query_vars"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/pass-variables-via-get_template_part-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to Pass Variables via get_template_part in WordPress"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9623"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=9623"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9623\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/9624"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=9623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=9623"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=9623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}