When using this shortcode on page multiple times, every shortcode gets the output of the addpad class and the inline style. How do I do this so only the shortcode that has the attributes will output the code?
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]
Technically, isset($bg)
will return true because in these lines, you specify a default for $bg
if it isn't set:
extract(shortcode_atts(array(
'bg' => '', // $bg default is '' if it wasn't found
'text' => '', // $text default is '' if it wasn't found
), $atts));
Instead of checking if it is set, check if it equals your default value:
if (strcmp($bg,'') == 0 || strcmp($text,'') == 0 ) {
// Do Something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.