How to passing variables to get_template_part() In WordPress

0
24

When we create a WordPress theme then some times need to pass a variable to another template for some functionality and save time, reduce duplicate code purpose, but I have check this feature not available in get_template_part() function.This function doesn’t accept other arguments and not allow to passing variables. For this requirement we have other solutions….

Method 1:

You can use locate_template() function in the place of get_template_part(), this function work with PHP include() function like as:

All of the variables available in your main template file will be available in that template file now too.

Method 2:

Simply have to declare your variable as global:

you can call this variable on other template like as:

Method 3:

For example You want to pass $custom_var to the template part at “content-page.php” , Follow like as:

Get value on content-page.php by using get_query_var()

LEAVE A REPLY

Please enter your comment!
Please enter your name here