Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ShortCodeAPI使用不同的属性更改返回_Php_Wordpress - Fatal编程技术网

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>