Php ShortCodeAPI使用不同的属性更改返回
我有一个短代码,我希望在短代码中添加某个属性后,它能传递另一个类。你是怎么做到的?或者最好的方法是什么 短代码:Php ShortCodeAPI使用不同的属性更改返回,php,wordpress,Php,Wordpress,我有一个短代码,我希望在短代码中添加某个属性后,它能传递另一个类。你是怎么做到的?或者最好的方法是什么 短代码: function one_half_columns($atts, $content = null){ $type = shortcode_atts( array( 'default' => 'col-md-6', 'push' => 'col-xs-6' ), $atts ); return '<div cl
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');
function one\u half\u列($atts,$content=null){
$type=shortcode_atts(数组(
“默认设置”=>“col-md-6”,
“推送”=>“col-xs-6”
)(港币),;
返回“”。do_短代码($content)。“”;;
}
添加_短码('one_half','one_half_columns');
示例当wordpress用户输入
[one_half type=“push”]
时,我希望它在数组col-xs-6
中使用push
的值。您的示例有两个问题-您在短代码中传递了一个“type”参数,但在短代码中需要“default”和“push”参数。您要做的是将shortcode_atts()
的结果分配给$atts
,然后在$atts['type']
上使用if
语句或开关
大小写
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' );
您应该获得以下输出:
<div class="col-xs-6">my content</div>
我的内容
<div class="col-xs-6">my content</div>