2014-10-31 41 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

相关问题