我已经看到了几个关于这个主题的问题,但似乎没有一个真正回答这个问题。
我当前正在上注册我的短代码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>