如何将值传递给 add_action() 闭包?
How to pass a value to an add_action() closure?
这是我的代码
<?php
$usersfollowing=1;
echo($usersfollowing);
add_action( 'elementor/query/my_custom_filter', function( $query ) {
$query->set( 'author', '1' );
} );
?>
当我在 'author' 之后给出值“1”时,它工作正常。我想使用 $usersfollowing 将值作为 1 传递。当我这样做时 $query->set( 'author', $usersfollowing )
它不起作用。
我想使用 $usersfollowing 来传递变量值。
$usersfollowing = 1;
echo($usersfollowing);
add_action('elementor/query/my_custom_filter', function ($query) use ($usersfollowing) {
$query->set('author', $usersfollowing);
});
正如 Nico Haase 在评论中指出的那样:
In PHP, what is a closure and why does it use the "use" identifier?
这是我的代码
<?php
$usersfollowing=1;
echo($usersfollowing);
add_action( 'elementor/query/my_custom_filter', function( $query ) {
$query->set( 'author', '1' );
} );
?>
当我在 'author' 之后给出值“1”时,它工作正常。我想使用 $usersfollowing 将值作为 1 传递。当我这样做时 $query->set( 'author', $usersfollowing )
它不起作用。
我想使用 $usersfollowing 来传递变量值。
$usersfollowing = 1;
echo($usersfollowing);
add_action('elementor/query/my_custom_filter', function ($query) use ($usersfollowing) {
$query->set('author', $usersfollowing);
});
正如 Nico Haase 在评论中指出的那样: In PHP, what is a closure and why does it use the "use" identifier?