This great answer David Gard让我有一个代谢箱sticky
(复选框)用于一次只能在循环中的一个帖子上设置(选中)的帖子。如果帖子A选中了那个元框,那么B,C。。。Z不能检查它。为了能够选中其他帖子中的框,用户必须首先取消选中帖子A中的框,然后才能选中其他框中的框。这本身就很有效。然而,我在使用其他元框(文本输入字段)时遇到了问题。每个帖子都有三个元数据库:sticky
和两个输入文本字段review
和source
(其他可能稍后添加)。
假设帖子Asticky
选中,值为review
和source
没关系,它们甚至可以是空的。保存这篇文章效果很好,我可以随意更改所有三个元框的值,没有任何错误或错误行为。但是,当我编辑source
或review
然后突然保存该帖子sticky
在帖子A中未设置。我不明白这些事情是如何联系在一起的。以下是三个元框的代码:
/*
* STICKY POSTS
*
*/
function add_sticky_metabox(){
add_meta_box(
\'sticky_post_metabox\', \'Sticky Post\', \'output_sticky_metabox\', \'post\'
);
}
add_action(\'add_meta_boxes\', \'add_sticky_metabox\');
// Make a post sticky
function output_sticky_metabox($post){
/** Grab the current \'my_sticky_post\' option value */
$sp = intval(get_option(\'sticky_post\'));
/** Check to see if the \'my_sticky_post\' option should be disabled or checked for the current Post */
$checked = checked($sp, $post->ID, false);
if($sp > 0) :
$disabled = (!disabled($sp, $post->ID, false)) ? \'disabled="true"\' : \'\';
else :
$disabled = \'\';
endif;
/** Add a nonce field */
wp_nonce_field(\'sticky_post_metabox\', \'sticky_post_metabox_nonce\');
/** Add a hidden field to check against in case it is unchecked before save */
$value = ($checked) ? \'1\' : \'0\';
echo \'<input type="hidden" name="was_checked" value="\' . $value . \'" />\';
/** Output the checkbox and label */
echo \'<label for="sticky_post">\';
echo \'<input type="checkbox" id="sticky_post" name="sticky_post" value="\' . $post->ID . \'" \' . $checked . $disabled . \'>\';
echo \'Make highlight?</label>\';
/** Let the user know which Post is currently sticky */
switch($sp) :
case 0:
$message = \'There\\\'s no highlight.\';
break;
case $post->ID:
$message = \'This post is the highlight!\';
break;
default:
$message = \'<a href="\' . get_edit_post_link($sp) . \'" title="\' . the_title_attribute(\'before=Bewerk bericht \\\'&after=\\\'&echo=0\') . \'">\' . get_the_title($sp) . \'</a> is currently lit\';;
endswitch;
echo \'<p><em>\' . $message .\'</em></p>\';
}
function save_sticky_metabox($post_id){
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
/** Ensure that a nonce is set */
if(!isset($_POST[\'sticky_post_metabox_nonce\'])) :
return;
endif;
/** Ensure that the nonce is valid */
if(!wp_verify_nonce( $_POST[\'sticky_post_metabox_nonce\'], \'sticky_post_metabox\')) :
return;
endif;
/** Ensure that an AUTOSAVE is not taking place */
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
return;
endif;
/** Ensure that the user has permission to update this option */
if(!current_user_can(\'edit_post\', $post_id)) :
return;
endif;
/**
* Everything is valid, now the option can be updated
*/
/** Check to see if the \'my_sticky_post\' option was checked */
if(isset($_POST[\'sticky_post\'])) : // It was...
update_option(\'sticky_post\', $_POST[\'sticky_post\']); // Update the option
else : // It was not...
/** Check to see if the option was checked prior to the options being updated */
if(isset($_POST[\'was_checked\'])) : // It was...
update_option(\'sticky_post\', 0); // Set the option to \'0\'
endif;
endif;
}
add_action(\'save_post\', \'save_sticky_metabox\');
/*
* Source
*
*/
function add_source_metabox(){
add_meta_box(
\'source_post_metabox\', \'Bron\', \'output_source_metabox\', \'post\'
);
}
add_action(\'add_meta_boxes\', \'add_source_metabox\');
function output_source_metabox($post){
wp_nonce_field(\'source_post_metabox\', \'source_post_metabox_nonce\');
$post_source = $post->post_source;
echo \'<label for="source_post">\';
echo \'<input type="text" id="source_post" name="source_post" value="\'.$post_source.\'" style="width: 80%;max-width: 720px;">\';
echo \' Add a source to your post.</label>\';
echo \'<p>E.g. <em>http://tweakers.net/nieuws/101372/ing-belgie-wil-betalingsgedrag-van-klanten-meer-gebruiken-voor-dienstverlening.html</em></p>\';
}
function save_source_metabox($post_id){
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
/** Ensure that a nonce is set */
if(!isset($_POST[\'source_post_metabox_nonce\'])) :
return;
endif;
/** Ensure that the nonce is valid */
if(!wp_verify_nonce( $_POST[\'source_post_metabox_nonce\'], \'source_post_metabox\')) :
return;
endif;
/** Ensure that an AUTOSAVE is not taking place */
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
return;
endif;
/** Ensure that the user has permission to update this option */
if(!current_user_can(\'edit_post\', $post_id)) :
return;
endif;
// Update and save the field so it can be used in our template
if ( isset( $_POST[\'source_post\'] ) ) {
$data = sanitize_text_field( $_POST[\'source_post\'] );
update_post_meta( $post_id, \'post_source\', $data );
}
}
add_action(\'save_post\', \'save_source_metabox\');
/*
* Reviews name field
*
*/
function add_review_metabox(){
add_meta_box(
\'review_post_metabox\', \'Review\', \'output_review_metabox\', \'post\'
);
}
add_action(\'add_meta_boxes\', \'add_review_metabox\');
function output_review_metabox($post){
wp_nonce_field(\'review_post_metabox\', \'review_post_metabox_nonce\');
$post_review = $post->post_review;
echo \'<label for="review_post">\';
echo \'<input type="text" id="review_post" name="review_post" value="\'.$post_review.\'" style="width: 80%;max-width: 720px;">\';
echo \' Add the name of the reviewed product.</label>\';
echo \'<p>E.g. <em>Lumia 930</em></p>\';
}
function save_review_metabox($post_id){
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
/** Ensure that a nonce is set */
if(!isset($_POST[\'review_post_metabox_nonce\'])) :
return;
endif;
/** Ensure that the nonce is valid */
if(!wp_verify_nonce( $_POST[\'review_post_metabox_nonce\'], \'review_post_metabox\')) :
return;
endif;
/** Ensure that an AUTOSAVE is not taking place */
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
return;
endif;
/** Ensure that the user has permission to update this option */
if(!current_user_can(\'edit_post\', $post_id)) :
return;
endif;
// Update and save the field so it can be used in our template
if ( isset( $_POST[\'review_post\'] ) ) {
$data = sanitize_text_field( $_POST[\'review_post\'] );
update_post_meta( $post_id, \'post_review\', $data );
}
}
add_action(\'save_post\', \'save_review_metabox\');
因此,在保存一篇帖子时,这会影响
sticky
其他人的。但我不明白这是怎么发生的,也不明白为什么会发生。有什么想法吗?