【问题标题】:How can I set and get the values of a multiple select with the Wordpress settings API for a theme options page?如何使用主题选项页面的 Wordpress 设置 API 设置和获取多项选择的值?
【发布时间】:2013-08-01 23:21:36
【问题描述】:

我想使用设置 API 为主题选项页面中的字段使用“多选字段”。

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

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>"
}

如何从选择元素中选择多个值并保存。

任何帮助将不胜感激。

【问题讨论】:

  • 你能贴一些代码吗..?

标签: wordpress wordpress-theming


【解决方案1】:

尝试使用

&lt;select name='plugin_options[clusters][]' multiple='multiple'&gt;

相反。这将确保 clusters 值保存为数组而不是字符串

【讨论】:

  • 谢谢,它成功了。我可以将多选转换为多个复选框吗?因为它将更加用户友好
  • 当然,因为您的示例中只有四个选项,所以它不会失控...如果您有很多选项可供选择,那里有一些 jQuery 插件您可以查看它们是否适合您harvesthq.github.io/chosengithub.com/vicb/bsmSelect
  • 这也适用于没有插件的复选框,只需 &lt;input type='checkbox' name='plugin_options[clusters][]' value='VALUE' /&gt;
猜你喜欢
  • 1970-01-01
  • 2021-03-16
  • 2014-07-28
  • 1970-01-01
  • 2015-03-08
  • 2015-03-13
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多