2017-05-08 82 views
0

当在页面上多次使用此短代码时,每个短代码都会获得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] 
+0

您是否有'one_third_last'的简码? – Chris

+0

是的,抱歉,我没有看到此评论。我只是将最后一个更改为one_third,仍然有同样的问题。 –

回答

0

技术上,因为在这些线路中,可以指定$bg默认的,如果没有设置isset($bg)将返回true您的默认值:

if (strcmp($bg,'') == 0 || strcmp($text,'') == 0) { 
    // Do Something 
} 
+0

有了这个改变,我仍然看到了同样的结果。所有三个“块”仍然获得类加法器。 –

+0

@RobertL你是否改变了两个if语句?他们是否仍然获得添加的背景颜色/文本颜色样式? – Chris

+0

是的,我做到了。红色的bg正在通过,但所有三个都有addpad类。 '函数block_one_third($的ATT,$含量= NULL){ 提取物(shortcode_atts(阵列( 'BG'=> '', '文本'=> '', ),$的ATT)); (strcmp($ bg,'')== 0 || strcmp($ text,'')== 0){$ style ='style =“background-color:'。$ bg。'; color:' 。$ text。''';} if(strcmp($ bg,'')== 0 || strcmp($ text,'')== 0){$ padit ='addpad'; } return'

' . do_shortcode($content) . '
'; } add_shortcode('one_third','block_one_third');' –