如何在visual composer中添加多选类别

时间:2017-11-19 16:30:03

标签: wordpress categories

如何为Visual Composer添加多重选择器?

vc_map( array(
"name" => esc_html__("Ajax Posts", '7mag'),
"base" => "YT_ajaxpost",
"icon" => "YT_vc_ico_ajaxpost",
"class" => "YT_vc_sc_ajaxpost",
"category" => esc_html__("7mag", '7mag'),
"params"    => array(
array(
        "type" => "dropdown",
        "heading" => esc_html__("Style", '7mag'),
        "param_name" => "multiple",
        "value" => $categories_array,
    ),
) );

例如: link

2 个答案:

答案 0 :(得分:5)

您需要注册新的短代码属性。

// Create multi dropdown param type
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );
function dropdown_multi_settings_field( $param, $value ) {
   $param_line = '';
   $param_line .= '<select multiple name="'. esc_attr( $param['param_name'] ).'" class="wpb_vc_param_value wpb-input wpb-select '. esc_attr( $param['param_name'] ).' '. esc_attr($param['type']).'">';
   foreach ( $param['value'] as $text_val => $val ) {
       if ( is_numeric($text_val) && (is_string($val) || is_numeric($val)) ) {
                    $text_val = $val;
                }
                $text_val = __($text_val, "js_composer");
                $selected = '';

                if(!is_array($value)) {
                    $param_value_arr = explode(',',$value);
                } else {
                    $param_value_arr = $value;
                }

                if ($value!=='' && in_array($val, $param_value_arr)) {
                    $selected = ' selected="selected"';
                }
                $param_line .= '<option class="'.$val.'" value="'.$val.'"'.$selected.'>'.$text_val.'</option>';
            }
   $param_line .= '</select>';

   return  $param_line;
}

然后使用它:

array(
        "type" => "dropdown_multi",
        "heading" => esc_html__("Style", '7mag'),
        "param_name" => "multiple",
        "value" => $categories_array,
    ),

答案 1 :(得分:0)

我已经更新了上述解决方案。由于 $value!=='' 检查保存后未选择值。

function dropdown_multi_settings_field( $param, $value ) {
    if ( ! is_array( $value ) ) {
        $param_value_arr = explode( ',', $value );
    } else {
        $param_value_arr = $value;
    }

    $param_line  = '';
    $param_line .= '<select multiple name="' . esc_attr( $param['param_name'] ) . '" class="wpb_vc_param_value wpb-input wpb-select ' . esc_attr( $param['param_name'] ) . ' ' . esc_attr( $param['type'] ) . '">';
    foreach ( $param['value'] as $text_val => $val ) {
        if ( is_numeric( $text_val ) && ( is_string( $val ) || is_numeric( $val ) ) ) {
            $text_val = $val;
        }
        $selected = '';
        if ( ! empty( $param_value_arr ) && in_array( $val, $param_value_arr ) ) {
            $selected = ' selected="selected"';
        }
        $param_line .= '<option class="' . $val . '" value="' . $val . '"' . $selected . '>' . $text_val . '</option>';
    }
    $param_line .= '</select>';

    return $param_line;
}
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );