[英]How do I get values pre-selected in a WordPress Multi select Box (wp_dropdown_categories)?
I am trying to get the following wp_dropdown_categories call to pre select values based on what the user has submitted before. 我试图获取以下wp_dropdown_categories调用,以根据用户之前提交的内容预选择值。 This is part of a larger user profile edit form.
这是较大的用户个人资料编辑表单的一部分。 The values are pulled in just fine but are not being automatically highlighted if they were selected previously.
这些值可以很好地拉入,但如果先前已选择,则不会自动突出显示。 Any help would be appreciated!!
任何帮助,将不胜感激!! job_ind_pref_call=custom user profiel field.
job_ind_pref_call =自定义用户配置文件字段。
</label> <?php
$sel = 0;
$sel1 = get_user_meta($user_ID, 'job_ind_pref_call', true);
if(isset($_POST['job_ind_pref_call'])) {
$sel1 = $_POST['job_ind_pref_call'];
}
if (isset($posted['job_term_cat']) && $posted['job_term_cat']>0) $sel = $posted['job_term_cat'];
global $featured_job_cat_id;
$args = array(
'orderby' => 'name',
'exclude' => 3,
'order' => 'ASC',
'name' => 'job_ind_pref_call[]',
'hierarchical' => 1,
'echo' => 0,
'class' => 'job_cat',
'selected' => $sel1,
'taxonomy' => 'job_cat',
'hide_empty' => false
);
$dropdown = wp_dropdown_categories( $args );
$dropdown = str_replace('class=\'job_cat\' >','class=\'job_cat\' multiple="multiple" size="6" onClick=GetMDDselections("job_ind_pref_call") ><option value="">'.__('Select a Line…', 'colabsthemes').'</option>',$dropdown);
echo $dropdown;
?> </p>
First of all, the WordPress crew recommends that you use wp_category_checklist() for multiple values. 首先,WordPress工作人员建议您将wp_category_checklist()用于多个值。
If you still persist to use wp_dropdown_categories()
you must be prepared to use an.. ehum, unofficial way to make the selected options work. 如果您仍然坚持使用
wp_dropdown_categories()
,则必须准备使用.. umum这种非官方的方法来使选定的选项起作用。
Here's what you need to do: 这是您需要做的:
1- Pass a new custom argument AND a new Walker class (we'll define it in step 2) to the wp_dropdown_categories()
function. 1-将新的自定义参数和新的Walker类(我们将在步骤2中定义)传递给
wp_dropdown_categories()
函数。 Let's say we call the function as such: 假设我们这样调用函数:
<?php
wp_dropdown_categories( array(
'_selected' => $selected_cats_arr,
'walker' => 'CategoryDropdownMultiple'
) );
?>
2- Create a new Walker class that is configured to select the option based on our new custom argument. 2-创建一个新的Walker类,该类配置为根据新的自定义参数选择选项。 The code for the Walker is based on
Walker_CategoryDropdown
defined in wp-includes/category-template.php . Walker的代码基于wp-includes / category-template.php中定义的
Walker_CategoryDropdown
。
<?php
class Walker_CategoryDropdownMultiple extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
function start_el( &$output, $category, $depth, $args, $id = 0 ) {
$pad = str_repeat(' ', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
$output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
// HERE IS THE ONLY CHANGE FROM THE ORIGINAL FILE
// We check our custom parameter which is an array instead of a single value.
if ( isset( $args['_selected'] ) && in_array( $category->term_id, $args['_selected'] ) )
$output .= ' selected="selected"';
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= ' ('. $category->count .')';
$output .= "</option>\n";
}
}
?>
Notes: 笔记:
wp_dropdown_categories()
and beyond. wp_dropdown_categories()
及更高版本中的单个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.