【问题标题】:How to pass a value to an add_action() closure? [duplicate]如何将值传递给 add_action() 闭包? [复制]
【发布时间】:2020-11-09 11:26:30
【问题描述】:

这是我的代码

<?php
$usersfollowing=1;
echo($usersfollowing);

            add_action( 'elementor/query/my_custom_filter', function( $query ) {
    $query->set( 'author', '1' );
} );
?>

当我在“作者”之后给出值“1”时,它工作正常。我想使用 $usersfollowing 将值传递为 1。当我执行 $query-&gt;set( 'author', $usersfollowing ) 时,它不起作用。

我想使用 $usersfollowing 来传递变量值。

【问题讨论】:

  • 我怀疑您需要function($query) use ($usersfollowing) { 将变量带入本地函数范围。

标签: php elementor


【解决方案1】:
$usersfollowing = 1;
echo($usersfollowing);

add_action('elementor/query/my_custom_filter', function ($query) use ($usersfollowing) {
    $query->set('author', $usersfollowing);
});

正如 Nico Haase 在 cmets 中指出的那样: In PHP, what is a closure and why does it use the "use" identifier?

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 2016-09-10
    • 2015-11-18
    • 2017-01-26
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    相关资源
    最近更新 更多