2014-10-31 43 views
0

我有一個簡碼,我希望它在傳遞特定屬性添加到簡碼時傳遞不同的類。你是怎樣做的?或者做到這一點的最佳方法是什麼?簡碼API更改具有不同屬性的返回

簡碼:

function one_half_columns($atts, $content = null){ 
    $type = shortcode_atts(array(
     'default' => 'col-md-6', 
     'push' => 'col-xs-6' 
    ), $atts); 

    return '<div class="' . $type['push'] . '">' . do_shortcode($content) . '</div>';; 
} 
add_shortcode('one_half', 'one_half_columns'); 

實施例時WordPress用戶輸入[one_half type="push"]我希望它使用的push值在陣列col-xs-6

回答

1

你是一個例子,有幾個問題 - 你在短代碼中傳遞了「type」參數,但是在短代碼中需要引用「default」和「push」。您要做的是將shortcode_atts()的結果分配到$atts,然後使用if陳述或switch case $atts['type'];

function one_half_columns($atts, $content = null){ 
    // populate $atts with defaults 
    $atts = shortcode_atts(array(
     'type' => 'default' 
    ), $atts); 

    // check the value of $atts['type'] to set $cssClass 
    switch($atts['type']){ 
     case 'push': 
      $cssClass = 'col-xs-6'; 
      break; 
     default: 
      $cssClass = 'col-md-6'; 
      break; 
    } 

    return '<div class="' . $cssClass . '">' . do_shortcode($content) . '</div>'; 
} 
add_shortcode('one_half', 'one_half_columns'); 

現在,當你撥打:

[one_half type="push"]my content[/one_half] 

你應該得到的輸出:

<div class="col-xs-6">my content</div> 
+0

謝謝主席先生。 :) – Xrait 2014-11-01 18:26:53

相關問題