當在頁面上多次使用此短代碼時,每個短代碼都會獲得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,仍然有同樣的問題。 –