在页面上多次使用此短代码时,每个短代码都会获得addpad类的输出和内联样式。我该怎么做才能只有具有属性的短代码输出代码?
function block_one_third( $atts, $content = null ) {
extract(shortcode_atts(array(
'bg' => '',
'text' => '',
), $atts));
if (isset($bg) || isset($text)){ $style = ' style="background-color:'. $bg .';color:'. $text .'"';}
if (isset($bg) || isset($text)) { $padit = ' addpad'; }
return '<div class="one_third'. $padit .'"'. $style .'>' . do_shortcode($content) . '</div>';
}
add_shortcode('one_third', 'block_one_third');
[one_third]
Block 1 - Should have NO additional class or inline style
[/one_third]
[one_third bg="red"]
Block 2 WITH additional html output
[/one_third]
[one_third_last]
Block 3 - Should have NO additional class or inline style
[/one_third_last]
答案 0 :(得分:0)
从技术上讲,isset($bg)
将返回true,因为在这些行中,如果未设置,则指定$bg
的默认值:
extract(shortcode_atts(array(
'bg' => '', // $bg default is '' if it wasn't found
'text' => '', // $text default is '' if it wasn't found
), $atts));
检查它是否等于默认值,而不是检查它是否已设置:
if (strcmp($bg,'') == 0 || strcmp($text,'') == 0 ) {
// Do Something
}