我在由複選框處理的帖子中創建了自定義字段「標題」。現在我希望在保存帖子並選中複選框時,其他帖子中的所有「標題」複選框都將被清空。如果這個工作正常,那麼應該只有一個其他的帖子,選中該複選框。Wordpress獨特複選框
function createHeadlineField()
{
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
$value = get_post_meta($post_id, '_headline_field', true);
wp_nonce_field('headline_nonce_'.$post_id, 'headline_nonce');
?>
<div class="misc-pub-section misc-pub-section-last">
<label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="_headline_field" /><?php _e('This post is the top Story', 'pmg'); ?></label>
</div>
<?php
}
function saveHeadlineField($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (
!isset($_POST['headline_nonce']) ||
!wp_verify_nonce($_POST['headline_nonce'], 'headline_nonce_'.$post_id)
) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['_headline_field'])) {
update_post_meta($post_id, '_headline_field', $_POST['_headline_field']);
} else {
delete_post_meta($post_id, '_headline_field');
}
}
大家有一個線索如何做到這一點?我想我必須使用_headline_field值查詢帖子的帖子,並在更新實際帖子之前刪除這些帖子。
THX
嗨,一些代碼來看看將幫助;) –