我编写了一个小函数,它将启用自身的短代码。它应该能很好地解决你的问题。这对我非常有帮助。
打开文件“wp includes/shortcode”。并将do\\u shortcode()函数(第201行)更改为
function do_shortcode($content) {
global $shortcode_tags;
if (empty($shortcode_tags) || !is_array($shortcode_tags))
return $content;
$content = enable_recursive_shortcodes($content);
$pattern = get_shortcode_regex();
return preg_replace_callback( "/$pattern/s", \'do_shortcode_tag\', $content );
}
所有更改都是添加*$内容=enable\\u recursive\\u短代码($内容)*
该函数遍历传递给它的$内容,并为其内部的短代码的短代码名称添加后缀。然后只处理外部的短码,当处理它们的内容时,后缀将被删除,从而允许处理它们。它的深度是无限的。(示例:[框][框][框][框]内容][框][框][框])
在do\\u shortcode()函数上方添加以下函数
/**
* Add shortcode suffix enabling recursive shortchodes.
*
* This function goes inside the set of $recursive_tags and adds a suffix to
* the shortcode name so it will allow a shortcode to call itself in its content
*
* @since 2.5
* @return string Content with recursive shortcodes modified out.
*/
function enable_recursive_shortcodes($content) {
$recursive_tags = array( \'column\', \'div\', \'span\', \'table\' ); // define recursive shortcodes, these must have closing tags
$suffix = \'__recursive\'; // suffix is added to the tag of recursive shortcodes
$content = str_replace( $suffix, \'\', $content ); // remove old suffix on shortcodes to start process over
foreach( $recursive_tags as $recursive_tag ) {
$open_tag = \'[\' . $recursive_tag . \' \';
$close_tag = \'[/\'. $recursive_tag . \']\';
$open_tags = 0;
$open_pos = stripos( $content, $open_tag ); // find first opening tag
$offset = $open_pos + strlen( $open_tag ); // set offset for first closing tag
while( $open_pos !== false ) {
$close_pos = stripos( $content, $close_tag, $offset ); // find first closing tag
if(++$open_tags > 1) { // if we are inside an open tag
// modify open tag from original shortcode name
$content = substr( $content, 0, $open_pos ) .
\'[\' .$recursive_tag . $suffix . \' \' .
substr( $content, $offset );
// modify closing tag from original shortcode name
$content = substr( $content, 0, $close_pos + strlen($suffix) ) .
\'[/\'.$recursive_tag.\'__recursive]\' .
substr( $content, $close_pos + strlen( $close_tag ) + strlen($suffix) );
$open_tags--;
}
$open_pos = stripos( $content, $open_tag, $offset ); // find next opening tag
if( $close_pos < $open_pos ) $open_tags--; // if closing tag comes before next opening tag, lower open tag count
$offset = $open_pos + strlen($open_tag); // set new offset for next open tag search
}
}
return $content;
}
请确保根据需要更新$recursive\\u tags变量。只有这些标记才会检查同名子项。这些标记必须有结束标记[列][列],因为[列/]不起作用。由于函数在标记中的搜索方式,它们在开始标记中的短代码名称后面必须有空格,而在结束标记中必须没有空格。如果有人更新代码以允许[列]或[列/]样式的短代码工作,请共享。