如何使用主题选项页面的 Wordpress 设置 API 设置和获取多项选择的值? [英] How can I set and get the values of a multiple select with the Wordpress settings API for a theme options page?

查看:19
本文介绍了如何使用主题选项页面的 Wordpress 设置 API 设置和获取多项选择的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用设置 API 为主题选项页面中的字段使用多选字段".

I want to use a 'multiple select field' for a field in the theme option page using settings API.

我尝试使用以下代码但无法保存所有选定的值,它只保存最后一个选定的值.

I tried with the below code but unable to save all the selected values, it only saves last one selected.

add_action('admin_menu', 'create_theme_options_page');

function create_theme_options_page() {  
add_options_page('Theme Options', 'Theme Options', 'administrator', 'inc/site-options.php', 'build_options_page');
}

add_action('admin_init', 'register_and_build_fields');

function register_and_build_fields() {   
register_setting('plugin_options', 'plugin_options', 'validate_setting');
add_settings_field('clusters', 'Choose Clusters to show:', 'cluster_setting', __FILE__, 'site_section');
} 

function cluster_setting() {  
$options = get_option('plugin_options');

echo "<select name='plugin_options[clusters]' multiple='multiple'>
<option value='location'>Location</option>
<option value='role'>Role</option>
<option value='functional'>Functional Area</option>
<option value='industry'>Industry</option>
</select>"
}

如何从 select 元素中选择多个值并保存它们.

How can I select multiple values from the select element and save them.

任何帮助将不胜感激.

推荐答案

尝试使用

发送“验证码”获取 | 15天全站免登陆