有没有一种干净的方法可以在Wordpress的默认行为之外获取短代码的属性值?我正在Wordpress安装之外编写一个脚本include(\'../wp-load.php\');
在一开始。
就我而言,我希望"caption"
属性[caption] shortcode
我发现最好的方法如下,但我对此很不满意。
原帖子内容:
[caption id="attachment_1" align="alignleft" width="150" caption="I\'m a wicked cool banana"]<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />[/caption]
从快捷码获取标题的脚本:
// prepare full content
$content = $content_post->post_content;
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
// test the HTML and get the part inside
if (preg_match("/<p class=\\"wp-caption-text\\">(.*)<\\\\/p>/", $content, $matches)) {
$caption = $matches[1];
}
但这不是最好的办法,不是吗?
请注意,我不是要获取图像的属性,而是要获取短代码的属性。
EDIT 1
正如s\\u ha\\u dum所建议的,我尝试使用
get_shortcode_regex()
, 但我得到了以下信息:
array (size=7)
0 =>
array (size=1)
0 => string \'[caption id="attachment_1" align="alignleft" width="150" caption="I\'m a wicked cool banana"]<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />[/caption]\' (length=394)
1 =>
array (size=1)
0 => string \'\' (length=0)
2 =>
array (size=1)
0 => string \'caption\' (length=7)
3 =>
array (size=1)
0 => string \' id="attachment_1" align="alignleft" width="150" caption="I\'m a wicked cool banana"\' (length=206)
4 =>
array (size=1)
0 => string \'\' (length=0)
5 =>
array (size=1)
0 => string \'<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />\' (length=169)
6 =>
array (size=1)
0 => string \'\' (length=0)
这意味着我仍然需要Regex$匹配项才能得到我想要的结果。