我正在为Wordpress制作主题选项,我正在尝试在一个设置字段中创建一个多重复选框选项。但是,只保存了一个复选框值。经过一番搜索,我意识到需要以不同于单个复选框的方式保存多个复选框。但我似乎无法找到关于如何做到这一点的直接答案。
这是我的标记。
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_date">
<div>
<span>Off</span>
<span>On</span>
</div>
</label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_author">
<div>
<span>Off</span>
<span>On</span>
</div>
</label>
<p class="input-description">Author</p>
</div>
这就是我为单个复选框做的事情。所以我的问题是如何保存所有提交的复选框并回显它们的检查值?根据我的研究,我需要将值保存在数组中并检查它是否在数组中,但我没有使用php,所以不要完全理解我在做什么。任何帮助将不胜感激。
编辑:
我已尝试应用可能重复的帖子中的答案。但这些答案似乎根本不起作用。应用这些答案后,两个复选框都不会保存。也许我以错误的方式应用它?
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
<label for="show_date"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
<label for="show_author"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Author</p>
</div>
<?php
if(!empty($_POST['mbpanel_display_section_options'])) {
foreach($_POST['mbpanel_display_section_options'] as $check) {
echo $check;
}
}
?>
就像我说我没有经验的PHP。
编辑:
根据我的理解,我需要将输入名称设置为
<input name="The section on which this option will be displayed[ID used to identify the field]" />
所以我的完整代码是
//Initialize the Display Options page by registering the Sections, Fields, and Settings
function mbpanel_initialize_display_options() {
// If the display options don't exist, create them
if( false == get_option( 'mbpanel_display_section_options' ) ) {
add_option( 'mbpanel_display_section_options' );
} // end if
// Register Display Options section
add_settings_section(
'display_settings_section_id', // ID used to identify this section and with which to register options
'Display Options', // Title to be displayed on the administration page
'mbpanel_display_options_callback', // Callback used to render the description of the section
'mbpanel_display_section_options' // Section on which to add this section of options
);
// Add Display Options section fields
add_settings_field(
'show_meta', // ID used to identify the field throughout the theme
'Meta', // The label to the left of the option interface element
'mbpanel_toggle_meta_callback', // The name of the function responsible for rendering the option interface
'mbpanel_display_section_options', // The section on which this option will be displayed
'display_settings_section_id' // The name of the section to which this field belongs
);
// Register Display Options fields
register_setting(
'mbpanel_display_section_options', // Group/Page this setting belongs to
'mbpanel_display_section_options' // ID of the field being registered
);
} // end mbpanel_initialize_display_options
add_action('admin_init', 'mbpanel_initialize_display_options');
/* ------------------------------------------------------------------------ *
* Display Options Section Callbacks
* ------------------------------------------------------------------------ */
// Display Options section description
function mbpanel_display_options_callback() {
echo '<p class="page-description">Select which areas of content you wish to display.</p>';
} // end mbpanel_display_options_callback
/* ------------------------------------------------------------------------ *
* Display Options Field Callbacks
* ------------------------------------------------------------------------ */
// Render the Display Options interface elements for toggling the visibility of the header element
function mbpanel_toggle_meta_callback() {
// First, we read the options collection
$options = get_option('mbpanel_display_section_options');
// Next, we update the name attribute to access this element's ID in the context of the display options array
// We also access the show_header element of the options collection in the call to the checked() helper function
?>
<div class="option-wrap">
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_date"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_author"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Author</p>
</div>
</div>
<p class="option-description">Choose which post meta info you wish to display.</p>
<?php
} // end mbpanel_toggle_meta_callback
设置输入字段,如
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta][]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
或类似
<input type='checkbox' name='myCB[]' value='cheese sauce'>
什么都不做
<?php
if(!empty($_POST['mbpanel_display_section_options'])) {
foreach($_POST['mbpanel_display_section_options'] as $check) {
echo $check;
}
}
?>
我甚至不理解它的意图。对不起,如果我在这里感到痛苦,但我花了一整天的时间试图让这个工作,我尝试过的一切都没有用,我似乎无法对这个具体案例应用类似的实例。当我读到“把它变成阵列”之类的东西时,我也不知道这意味着什么。
答案 0 :(得分:0)
您需要将复选框名称设为数组
<input type='checkbox' name='myCB[]' value='cheese sauce'>
然后你可以从$_POST['myCB']
数组(它是一个数组)中获取它们。