如何使用主题选项页面的 Wordpress 设置 API 设置和获取多项选择的值? [英] How can I set and get the values of a multiple select with the Wordpress settings API for a theme options page?
本文介绍了如何使用主题选项页面的 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.
任何帮助将不胜感激.
推荐答案
尝试使用
相反.这将确保将簇值保存为数组而不是字符串
instead. This will ensure that the clusters value is saved as an array instead of a string
这篇关于如何使用主题选项页面的 Wordpress 设置 API 设置和获取多项选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文