【问题标题】:Set a new value for $key => $value using WordPress shortcode_atts使用 WordPress shortcode_atts 为 $key => $value 设置一个新值
【发布时间】:2015-01-16 23:36:50
【问题描述】:

我正在尝试更改 $key => $value 对中的值,但似乎无法正常工作。

$atts = shortcode_atts( array(
            "type"     => FALSE,
            "size"     => FALSE
        ), $atts );


print_r($atts);

输出:

Array
(
    [type] => default
    [size] => default
)


if ( $atts['size'] == 'default' ) {
    $atts['size'] == false;
}

print_r($atts);

输出:

Array
(
    [type] => default
    [size] => default
)

基本上,我想做的是,如果“size”是“default”,则将值重置为 false 或空字符串。我做错了什么?

【问题讨论】:

    标签: php arrays wordpress shortcode


    【解决方案1】:

    您没有分配变量!见:

    $atts['size'] == false;
                //^^ See here, you don't assign anything
    

    你必须删除一个=,所以它看起来像这样:

    $atts['size'] = false;
    

    我也会在这里使用var_dump,因为如果你指定false并且你使用print_r,输出将是:

    Array ( [type] => default [size] => ) 
    

    var_dump 你看得更清楚:

    array(2) { ["type"]=> string(7) "default" ["size"]=> bool(false) }
    

    【讨论】:

    • 天啊,我真是个傻瓜。我不知道我怎么没看到。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多