我已经看到了几个关于这个主题的问题,但似乎没有一个真正回答这个问题。
我当前正在上注册我的短代码after_setup_theme
.
function mbe_register_shortcodes() {
require_once( PATH . \'/inc/shortcodes.php\' );
add_shortcode( \'font\', \'mbe_shortcode_font\' );
}
add_action( \'after_setup_theme\', \'mbe_register_shortcodes\' );
目录
Shortcodes.php
:
/**
* @param array $attributes
* @param String|null $content
*
* @return string
*/
function mbe_shortcode_font( $attributes = array(), String $content = null ) {
$default_attributes = array(
\'color\' => false,
\'weight\' => \'normal\',
\'align\' => \'left\'
);
$attributes = shortcode_atts( $default_attributes, $attributes );
if ( $attributes[\'color\'] !== false ) {
$attributes[\'color\'] = \'color\';
}
$html = \'\' . PHP_EOL;
$html .= \'<span class="\' . esc_attr( $attributes[\'color\'] . \' font-\' . $attributes[\'weight\'] . \' text-\' . $attributes[\'align\'] ) . \'">\' . $content . \'</span>\' . PHP_EOL;
return $html;
}
Sample Text:
[font align="center" weight="extra-bold"]YOUR LOCAL [font color="true"]FULL SERVICE[/font] REAL ESTATE OFFICE[/font]
[font align="center" weight="semi-bold-italic"]Our Realtors Know Real Estate![/font]
With the default code specified above, the output is like:
<span class=" font-extra-bold text-center">YOUR LOCAL [font color="true"]FULL SERVICE</span> REAL ESTATE OFFICE[/font]
<span class=" font-semi-bold-italic text-center">Our Realtors Know Real Estate!</span>
一
[font align="center" weight="extra-bold"]YOUR LOCAL [font color="true"]FULL SERVICE[/font] REAL ESTATE OFFICE[/font]
[font align="center" weight="semi-bold-italic"]Our Realtors Know Real Estate![/font]
#1 I\'ve tried...
$html .= \'<span class="\' . esc_attr( $attributes[\'color\'] . \' font-\' . $attributes[\'weight\'] . \' text-\' . $attributes[\'align\'] ) . \'">\' . $content. \'</span>\' . PHP_EOL;
return do_shortcode( $html );
#1 and outputs...
<span class=" font-extra-bold text-center">YOUR LOCAL
<span class="color font-normal text-left"></span>
FULL SERVICE</span>
REAL ESTATE OFFICE[/font]
<span class=" font-semi-bold-italic text-center">Our Realtors Know Real Estate!</span>
#2 And this...
$html .= \'<span class="\' . esc_attr( $attributes[\'color\'] . \' font-\' . $attributes[\'weight\'] . \' text-\' . $attributes[\'align\'] ) . \'">\' . do_shortcode( $content ) . \'</span>\' . PHP_EOL;
return $html;
#2 and outputs...
<span class=" font-extra-bold text-center">YOUR LOCAL
<span class="color font-normal text-left"></span>
FULL SERVICE</span>
REAL ESTATE OFFICE[/font]
<span class=" font-semi-bold-italic text-center">Our Realtors Know Real Estate!</span>
#3 And this...
$html .= \'<span class="\' . esc_attr( $attributes[\'color\'] . \' font-\' . $attributes[\'weight\'] . \' text-\' . $attributes[\'align\'] ) . \'">\' . do_shortcode( $content ). \'</span>\' . PHP_EOL;
return do_shortcode( $html );
#3 and outputs ...
<span class=" font-extra-bold text-center">YOUR LOCAL
<span class="color font-normal text-left"></span>
FULL SERVICE</span>
REAL ESTATE OFFICE[/font]
<span class=" font-semi-bold-italic text-center">Our Realtors Know Real Estate!</span>
最合适的回答,由SO网友:Michael Ecklund 整理而成
TLDR; 嵌套的短代码不能具有相同的名称。短代码中的短代码必须具有唯一的名称。
我找到了the answer 幸亏@patnz
响应:shortcode inside another shortcode.
如果你看看WordPress Codex :: Shortcode API :: Nested Shortcodes 您将通过一个示例找到答案。
所以在我的情况下,作为一个快速修复,我刚刚添加了另一个短代码。
function mbe_register_shortcodes() {
require_once( PATH . \'/inc/shortcodes.php\' );
add_shortcode( \'font\', \'mbe_shortcode_font\' );
add_shortcode( \'font_color\', \'mbe_shortcode_font_color\' ); // Added
}
add_action( \'after_setup_theme\', \'mbe_register_shortcodes\' );
Contents of shortcodes.php
:
function shortcode_font( $attributes = array(), String $content = null ) {
$default_attributes = array(
\'weight\' => \'normal\',
\'align\' => \'left\'
);
$attributes = shortcode_atts( $default_attributes, $attributes );
$html = \'<span class="font-\' . esc_attr( $attributes[\'weight\'] ) . \' text-\' . esc_attr( $attributes[\'align\'] ) . \'">\' . $content . \'</span>\';
return do_shortcode( $html );
}
function shortcode_font_color( $attributes = array(), String $content = null ) {
$html = \'<span class="color">\' . $content . \'</span>\';
return do_shortcode( $html );
}