2017-05-08 8 views
0

페이지에서이 단축 코드를 여러 번 사용할 때마다 모든 단축 코드가 addpad 클래스와 인라인 스타일 출력을받습니다. 속성을 가진 짧은 코드 만 코드를 출력하도록하려면 어떻게해야합니까? 가 동일한 경우Wordpress PHP 단축 코드

는 대신이 설정되어있는 경우 체크
extract(shortcode_atts(array(
    'bg' => '', // $bg default is '' if it wasn't found 
    'text' => '', // $text default is '' if it wasn't found 
), $atts)); 

확인 :이 설정되지 않은 경우이 라인에서, 당신은 $bg의 기본을 지정하기 때문에

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

기술적으로, isset($bg)는 true를 돌려줍니다 기본 값 :

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

이 변경 사항과 동일한 결과가 계속 표시됩니다. 세 개의 "블록"모두 여전히 클래스 추가 기능을 얻고 있습니다. –

+0

@RobertL if 문을 모두 변경 했습니까? 그들은 모두 여전히 배경색/텍스트 색상 스타일을 추가하고 있습니까? – Chris

+0

예. 붉은 색의 bg가 단지 하나만 지나가고 있지만 모두 세개가 addpad 클래스를 가지고 있습니다. '기능 block_one_third ($ ATTS, $ 함량 = NULL) { 추출물 (shortcode_atts (어레이 ( "BG'=> '' '문자'=> '' ) $의 ATTS을)); if (strcmp ($ bg, '') == 0 || strcmp ($ text, '') == 0) {$ style = "background-color : $ strcmp ($ text, '') == 0) {$ padit = 'addpad'; $ text. ' "}; } 돌아 가기 '

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