How to pass a variable value to another file in WordPress?

When passing the value of a variable to another PHP file in WordPress while using the ‘get_template_part()’ function, it’s simple to achieve this by using the ‘set_query_var()’ function:

set_query_var( string $query_var, mixed $value )

For example, if you have a template part named ‘slideshow’ and you want to send the category ID to this file, you can use the following code:

set_query_var( 'category', absint( '17' ) );
get_template_part( 'template-parts/slideshow' );

In this example, we are passing the category ID ’17’ to the ‘slideshow.php’ file located in the ‘template-parts’ folder.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.