是否可以嵌套相同短代码的wordpress短代码?

时间:2012-11-01 19:23:57

标签: php wordpress shortcode

我知道如果使用do_shortcode包装器就可以嵌套短代码,但是,codex声明:

“但是,如果使用短代码宏来封装另一个同名宏,则解析器将失败:”

有解决方法吗?

例如,如果我有一个短代码来制作一个div,例如:

[div]some content in a div[/div]

我希望能够使用:

[div]
    [div]a nested div[/div]
[/div]

但是使用标准的do_shortcode包装器会失败。

我的临时解决方法是将带有_parent的短代码复制到名称中,但我只能嵌套1级深,除非我创建了div_parent1,div_parent2等...

3 个答案:

答案 0 :(得分:3)

如果您正在编写短代码,则有一个简单的解决方案。您可以编写几个调用相同功能的短代码。我有创建html块的短代码,例如div,并且有几个像div,block1,block2这样的名字。

add_shortcode('div', 'devondev_block');
add_shortcode('block', 'devondev_block');
add_shortcode('block2', 'devondev_block');

他们都调用相同的功能。只要您记得使用不同的短代码,它们就可以嵌套。

WordPress短代码支持只会尝试使用regex进行解析。可以使用正则表达式,有限状态机和堆栈的混合来进行这种解析。这种方法可以处理嵌套,并且速度非常快,特别是在短代码很少的情况下。每次遇到这个我都试着尝试一下。

答案 1 :(得分:1)

API告诉它它是这样的,因此这是不可能的:

This is a limitation of the context-free regexp parser used by do_shortcode() - it is very fast but does not count levels of nesting, so it can't match each opening tag with its correct closing tag in these cases.

最新版本(3.4.2)中的相关功能是:

function do_shortcode($content) {
    global $shortcode_tags;

    if (empty($shortcode_tags) || !is_array($shortcode_tags))
        return $content;

    $pattern = get_shortcode_regex();
    return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}

function get_shortcode_regex() {
    global $shortcode_tags;
    $tagnames = array_keys($shortcode_tags);
    $tagregexp = join( '|', array_map('preg_quote', $tagnames) );

    // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
    return
          '\\['                              // Opening bracket
        . '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
        . "($tagregexp)"                     // 2: Shortcode name
        . '\\b'                              // Word boundary
        . '('                                // 3: Unroll the loop: Inside the opening shortcode tag
        .     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
        .     '(?:'
        .         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
        .         '[^\\]\\/]*'               // Not a closing bracket or forward slash
        .     ')*?'
        . ')'
        . '(?:'
        .     '(\\/)'                        // 4: Self closing tag ...
        .     '\\]'                          // ... and closing bracket
        . '|'
        .     '\\]'                          // Closing bracket
        .     '(?:'
        .         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
        .             '[^\\[]*+'             // Not an opening bracket
        .             '(?:'
        .                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
        .                 '[^\\[]*+'         // Not an opening bracket
        .             ')*+'
        .         ')'
        .         '\\[\\/\\2\\]'             // Closing shortcode tag
        .     ')?'
        . ')'
        . '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}

答案 2 :(得分:0)

您需要再次为短代码中的内容执行短代码。例如:

add_shortcode('div', function($attributes, $content, $tag) {
    ...
    do_shortcode($content);
    ...
});

请参阅:What is the best way to enable nested shortcodes?(Wordpress SE)

刚看到:这并没有解决Wordpress中的问题所以不回答你的问题。我认为到现在为止它已经修好了。

相关问题