WordPress短代码只接受默认属性

问题描述 投票:0回答:1

我目前正在尝试编写自己的短代码,以便在我的WordPress网站上使用YouTube API。我的代码也有效,但只能使用短代码的默认属性。我的代码忽略了通过shortcode self附加的属性。

谁能帮我?我google了很多,但我没有找到这样的东西。我想我做了所有类似于codex的方式,但也许你知道的更多。这是我的代码:

function api_youtube_integration($atts, $content = null) {
    $a = shortcode_atts(array(
        'class'         => '',
        'videoCount'    => 5,
        'bottomBar'     => true
    ), $atts);
ob_start();
    ?>

    <div data-video="<?php echo esc_html($a['videoCount']); ?>">Test</div>
<?php 
    return ob_get_clean();
}
add_shortcode('youtubeAPI', 'api_youtube_integration');

我的短代码:[youtubeAPI videoCount = 12]代码只返回5作为videCount而不是12 ...

最好的问候卢卡斯

php wordpress attributes shortcode wordpress-shortcode
1个回答
0
投票

所以显然传递给shortcode_atts()的属性总是转换为小写。 Official docs:

在将属性名称传递到处理函数之前,它们始终会转换为小写。值不受影响。 [myshortcode FOO =“BAR”]产生$ atts = array('foo'=>'BAR')。

因此,您需要确保所有选项都是小写的,或者使用下划线来分隔单词。

© www.soinside.com 2019 - 2024. All rights reserved.