【发布时间】:2016-02-14 09:32:41
【问题描述】:
我的目的是从这样的事情出发,取自post_content:
[video width="1080" height="1920" webm="http://path/file.webm" autoplay="true"][/video]
到这样的数组:
Array(
width=>1080,
height=>1920,
webm=>"http://path/file.webm",
autoplay=>"true"
);
当然取决于用户在视频短代码中输入的内容,或多或少的配对。
我已经阅读了关于 shortcode_atts 的 Shortcode_API 和 instructions。我无法找到关于如何以数组形式获取这些属性的简单说明。
尽管人们一直建议我不能使用shortcode_atts,因为这个wordpress函数要求属性已经在一个数组中!
我知道如何使用正则表达式或多或少地完成上述操作。但是是否有任何 wordpress 明显的方法可以将简码属性转换为数组?我知道应该有。
例如,这是行不通的:
shortcode_atts( array(
'width' => '640',
'height' => '360',
'mp4' => '',
'autoplay' => '',
'poster' => '',
'src' => '',
'loop' => '',
'preload' => 'metadata',
'webm' => '',
), $atts);
因为 $atts 应该是一个数组,但我只有一个来自$post_content 的字符串,它看起来像这样:
[video width="1080" height="1920" webm="http://path/file.webm" autoplay="true"][/video]
请注意:我没有实现短代码功能或类似的功能。我只需要阅读在帖子内容中添加的 wordpress 视频短代码。
【问题讨论】:
-
如果您想要一个通用的解决方案来处理任何类型的短代码,请查看我的库:github.com/thunderer/Shortcode
标签: php wordpress attributes extract shortcode