默认情况下,无法将我的元字段复选框设置为true。
除了默认值之外,其他一切都正常工作。
我知道有很多问题和答案,
但我什么都试过了,还是没能得到。
这是我的代码:
function theme_add_meta_box() {
add_meta_box( \'theme_post_sidebar_option\', esc_html__( \'Additional Options\', \'theme\' ), \'theme_post_sidebar_settings\', \'post\', \'normal\', \'high\' );
}
add_action( \'add_meta_boxes\', \'theme_add_meta_box\' );
然后我的选择:
function theme_post_sidebar_settings($post) {
$sidebar = get_post_meta($post->ID, \'_theme_post_meta_sidebar\', true);
wp_nonce_field( \'theme_update_post_sidebar_settings\', \'theme_update_post_sidebar_nonce\' );
?>
<input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php checked($sidebar); ?> />
<label for="theme_post_meta_sidebar_field"><?php esc_html_e( \'Sidebar\', \'theme\' ); ?></label>
<?php
}
然后我的保存功能:
function theme_save_post_sidebar_settings($post_id, $post) {
$edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
if( !current_user_can( $edit_cap, $post_id )) {
return;
}
if( !isset( $_POST[\'theme_update_post_sidebar_nonce\']) || !wp_verify_nonce( $_POST[\'theme_update_post_sidebar_nonce\'], \'theme_update_post_sidebar_settings\' )) {
return;
}
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
if(array_key_exists(\'theme_post_meta_sidebar_field\', $_POST)) {
update_post_meta(
$post_id,
\'_theme_post_meta_sidebar\',
sanitize_text_field($_POST[\'theme_post_meta_sidebar_field\'])
);
} else {
update_post_meta(
$post_id,
\'_theme_post_meta_sidebar\', null);
}
}
add_action( \'save_post\', \'theme_save_post_sidebar_settings\', 10, 2 );
谁能帮帮我吗?
最合适的回答,由SO网友:Chetan Vaghela 整理而成
要在metabox中获得默认为true的复选框,可以使用checked
复选框的属性。因此,我对您的代码做了两处更改。
在选项中,我添加了新条件,默认情况下它将选中复选框,如果meta\\u值1,如果meta\\u值的更新值未选中且不为空,则它将取消选中复选框在save函数中,我将值从null更改为unchecked因此,默认情况下,复选框将处于选中状态,当您使用复选框保存时,如果取消选中,则将选中复选框,并且保存时将取消选中复选框。
选项功能:
function theme_post_sidebar_settings($post) {
$sidebar = get_post_meta($post->ID, \'_theme_post_meta_sidebar\', true);
wp_nonce_field( \'theme_update_post_sidebar_settings\', \'theme_update_post_sidebar_nonce\' );
$checked = \'checked\';
if($sidebar == \'unchecked\' && !empty($sidebar))
{
$checked = \'\';
}
?>
<input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php echo $checked; ?> />
<label for="theme_post_meta_sidebar_field"><?php esc_html_e( \'Sidebar\', \'theme\' ); ?></label>
<?php
}
保存功能:
function theme_save_post_sidebar_settings($post_id, $post) {
$edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
if( !current_user_can( $edit_cap, $post_id )) {
return;
}
if( !isset( $_POST[\'theme_update_post_sidebar_nonce\']) || !wp_verify_nonce( $_POST[\'theme_update_post_sidebar_nonce\'], \'theme_update_post_sidebar_settings\' )) {
return;
}
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
if(array_key_exists(\'theme_post_meta_sidebar_field\', $_POST)) {
update_post_meta(
$post_id,
\'_theme_post_meta_sidebar\',
sanitize_text_field($_POST[\'theme_post_meta_sidebar_field\'])
);
} else {
update_post_meta(
$post_id,
\'_theme_post_meta_sidebar\', \'unchecked\');
}
}
add_action( \'save_post\', \'theme_save_post_sidebar_settings\', 10, 2 );
SO网友:uPrompt
无法留下评论,因此我将留下您可能会找到的答案。
这是什么?
<?php checked($sidebar); ?>
这是一个返回“checked”或“nothing”的函数吗?
因为这会显示您的复选框,并且默认选中:
<input type="checkbox" name="theme_post_meta_sidebar_field"
id="theme_post_meta_sidebar_field" value="1" checked>
我想这会有用的。
$sidebar = get_post_meta($post->ID, \'_theme_post_meta_sidebar\', true);
if ($sidebar) {
$checkboxchecked = \'checked\';
} else {
$checkboxchecked = null;
}
wp_nonce_field( \'theme_update_post_sidebar_settings\', \'theme_update_post_sidebar_nonce\' );
?>
<input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="1" <?php $checkboxchecked; ?>/>
为了在默认情况下选中复选框,您必须在输入标记的末尾放置“checked”或“checked=“checked””。我只是猜测您的代码没有这样做。
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked
SO网友:HK89
您可以为添加以下代码段:
更改输入值“true”,然后添加<?php echo $sidebar?\'checked\': \'unchecked\'; ?>
输入元素中
function theme_post_sidebar_settings($post) {
$sidebar = get_post_meta($post->ID, \'_theme_post_meta_sidebar\', true);
wp_nonce_field( \'theme_update_post_sidebar_settings\', \'theme_update_post_sidebar_nonce\' );
?>
<input type="checkbox" name="theme_post_meta_sidebar_field" id="theme_post_meta_sidebar_field" value="true" <?php echo $sidebar?\'checked\': \'unchecked\'; ?> />
<label for="theme_post_meta_sidebar_field"><?php esc_html_e( \'Sidebar\', \'theme\' ); ?></label>
<?php
}
更改以下功能
function theme_save_post_sidebar_settings($post_id, $post) {
$edit_cap = get_post_type_object( $post->post_type )->cap->edit_post;
if( !current_user_can( $edit_cap, $post_id )) {
return;
}
if( !isset( $_POST[\'theme_update_post_sidebar_nonce\']) || !wp_verify_nonce( $_POST[\'theme_update_post_sidebar_nonce\'], \'theme_update_post_sidebar_settings\' )) {
return;
}
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
update_post_meta(
$post_id,
\'_theme_post_meta_sidebar\',
sanitize_text_field($_POST[\'theme_post_meta_sidebar_field\'])
);
}
add_action( \'save_post\', \'theme_save_post_sidebar_settings\', 10, 2 );