$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - How to unset a set query variable?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme development - How to unset a set query variable?

matteradmin7PV0评论

My theme file, multiple times over, passes sets a variable to a template part used in a subsequent template part file via set_query_var, like so...

set_query_var('feature_id', array(143866));
set_query_var('tax_meta_value',  'Payments');
get_template_part('partials/page-blocks/block_tag_new');

set_query_var('tax_meta_value',  'Venture Capital');
get_template_part('partials/page-blocks/block_tag_new');

In the template part, the variable is plucked out, using get_query_var, as $feature_id.

The only problem is - in the second instance here, for example - the template part still retains a memory of the first feature_id value passed above.

When I call the template part in the second instance, how do I ensure that no feature_id value is resident, since none is passed?

Do I need to do something like set the ensuing $feature_id, within the template part file, as global and unset it? Or do that but in the calling file, or something else?

My theme file, multiple times over, passes sets a variable to a template part used in a subsequent template part file via set_query_var, like so...

set_query_var('feature_id', array(143866));
set_query_var('tax_meta_value',  'Payments');
get_template_part('partials/page-blocks/block_tag_new');

set_query_var('tax_meta_value',  'Venture Capital');
get_template_part('partials/page-blocks/block_tag_new');

In the template part, the variable is plucked out, using get_query_var, as $feature_id.

The only problem is - in the second instance here, for example - the template part still retains a memory of the first feature_id value passed above.

When I call the template part in the second instance, how do I ensure that no feature_id value is resident, since none is passed?

Do I need to do something like set the ensuing $feature_id, within the template part file, as global and unset it? Or do that but in the calling file, or something else?

Share Improve this question edited Mar 3, 2019 at 22:32 Robert Andrews asked Mar 3, 2019 at 22:01 Robert AndrewsRobert Andrews 9881 gold badge19 silver badges42 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

When I call the template part in the second instance, how do I ensure that no feature_id value is resident, since none is passed?

I'm afraid you misunderstood what set_query_var really does. It doesn't pass anything and it doesn't work only for the next get_template_part call.

OK, so what does it really do? From docs:

Set query variable.

And here's its code:

function set_query_var( $var, $value ) {
    global $wp_query;
    $wp_query->set( $var, $value );
}

So now everything should be clear. So let's take a look at your original code:

set_query_var('feature_id', array(143866));  // <- sets query var called feature_id to 143866
set_query_var('tax_meta_value',  'Payments');  // <- set query var called tax_meta_value to Payments
get_template_part('partials/page-blocks/block_tag_new');

set_query_var('tax_meta_value',  'Venture Capital');  // <- sets tax_meta_value to Venture Capital 
// feature_id is still 143866, because it hasn't been changed
get_template_part('partials/page-blocks/block_tag_new');

If you want to unset the query var, you can set it to false/NULL:

set_query_var('feature_id', false);
Post a comment

comment list (0)

  1. No comments so far