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.
Leave a Reply