当在页面上多次使用此短代码时,每个短代码都会获得addpad类和内联样式的输出。我该如何做到这一点,只有具有属性的短代码才会输出代码?WordPress的PHP短代码,如果isset通过页面上的所有短代码
extract(shortcode_atts(array(
'bg' => '', // $bg default is '' if it wasn't found
'text' => '', // $text default is '' if it wasn't found
), $atts));
而不是检查,如果它被设置的,检查它是否等于:
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]
您是否有'one_third_last'的简码? – Chris
是的,抱歉,我没有看到此评论。我只是将最后一个更改为one_third,仍然有同样的问题。 –