EDIT 2
Multiple unnamed parameters, starting with equals sign.
function postintro_bb_shortcode( $atts ) {
// remove equals sign from first unnamed param
if ( isset( $atts[0] ) && strpos( $atts[0], \'=\' ) !== false ) {
$atts[0] = substr( $atts[0], 1 );
}
// Param array to string
$intro = is_array( $atts ) ? implode( \' \', $atts ) : \'\';
return \'<div class="postintro">\' . esc_html( $intro ) . \'</div>\';
}
add_shortcode( \'postintro_bb\', \'postintro_bb_shortcode\' );
【postintro\\u bb=在ex中的Vivamus sit amet turpis ultricies lorem Dignessim sollicitudin id】
Unnamed parameter with closing tag
function source_shortcode( $atts, $content = null ) {
// remove equals sign from first unnamed param
if ( isset( $atts[0] ) && strpos( $atts[0], \'=\' ) !== false ) {
$atts[0] = substr( $atts[0], 1 );
}
return \'<a href="\' . esc_url( $atts[0] ) . \'">\' . esc_html( $content ) . \'</a>\';
}
add_shortcode( \'source\', \'postintro_b_shortcode\' );
[来源=”http://www.example.com/“]测试[[来源]
<小时>
EDIT
这里有几种方法可以定义和使用自定义短代码。
一
function postintro_shortcode( $atts , $content = null ) {
return \'<div class="postintro">\' . $content . \'</div>\';
}
add_shortcode( \'postintro\', \'postintro_shortcode\' );
测试Self closing shortcode tag.
显然,未命名的参数也起作用
function postintro_b_shortcode( $atts ) {
$intro = ! empty( $atts[0] ) ? $atts[0] : \'\';
return \'<div class="postintro">\' . $intro . \'</div>\';
}
add_shortcode( \'postintro_b\', \'postintro_b_shortcode\' );
【postintro\\u b blahblah】Self-closing shortcode tag with named parameter.
function postintro_c_shortcode( $atts ) {
$intro = ! empty( $atts[\'intro\'] ) ? $atts[\'intro\'] : \'\';
return \'<div class="postintro">\' . $intro . \'</div>\';
}
add_shortcode( \'postintro_c\', \'postintro_c_shortcode\' );
[简介]<小时>
Original answer
因为您的短代码名称有括号,所以短代码API异常。改变add_shortcode( \'[postintro]\', \'postintro_shortcode\' );
至add_shortcode( \'postintro\', \'postintro_shortcode\' );