【发布时间】:2017-03-06 16:52:53
【问题描述】:
我在帖子中创建了一个由复选框处理的自定义字段“标题”。现在我希望当帖子被保存并选中复选框时,其他帖子中的所有“标题”复选框都被清空。如果这工作正常,那么应该只有另一个帖子选中了该复选框。
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 值的帖子并在更新实际帖子之前删除这些帖子。
谢谢
【问题讨论】:
-
嗨,看看一些代码会有所帮助;)