2017-05-08 80 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');' –