如何从帖子内容中提取WordPress短代码属性

时间:2013-12-17 02:23:27

标签: php html wordpress

在我在WordPress上的帖子中,我创建了一个未知数量的my_player类型的短代码,并正确添加了钩子。我想知道是否有某种类型的WordPress功能可以将您的内容传递给短代码名称,它可以为您提供一系列匹配的短代码,其属性由属性名称索引。像我下面的代码...

$matches = get_shortcode_matches($content, "my_player");

for($i = 0; $i < sizeof($matches); $i++)
{
    $title = $matches[$i]['title'];
    $mp3 = $matches[$i]['mp3'];
    $soundcloud = $matches[$i]['soundcloud'];
}

我知道当你使用add_shortcode()函数为短代码创建钩子时,你可以像我上面那样使用这些索引值,但是我需要有一个函数可以在以后和循环之外访问它们。 WordPress有没有这样的功能?

3 个答案:

答案 0 :(得分:3)

有几种方法可以做到这一点:  1.像上面一样编写自己的代码段,在“mu-plugins”文件夹中插入以下内容

// [myplayer attr="your-value"]
function myplayer_func($atts) {
    extract(shortcode_atts(array(
        'title' => 'something',
        'mp3' => 'another something',
                'soundcloud' => 'something else',
    ), $atts));

    return "attr= {$attr}";
}
add_shortcode('myplayer', 'myplayer_func');

然后

[myplayer title="something" mp3="another something" soundcloud="something else"]

来自任何地方的任何帖子,包括子域名。  2.您可以使用ShortcoderGlobal Content Blocks

等插件

答案 1 :(得分:0)

试试这个,

<?php do_shortcode($content); ?>

答案 2 :(得分:0)

我认为你不能使用现有的行动。 [shortcode_parse_atts]没有附加任何事件。 唯一可行的方法是在每个[add_shortcode]相关函数/方法中使用add_action / global。

类似的东西:

function doShortCode($atts, $content=null){
    global $my_shortcode_storage;
    $my_shortcode_storage['my_shortcode_1'][] = $atts;
}
相关问题