我的帖子类型“ebrd\\U视频”上有一个元框,它使用复选框将视频设置为“特色视频”。
我想将状态为“featured=on”的视频数量限制为1个。因此,当我将视频设置为特色视频并发布/更新该视频时,所有其他视频都设置为“特色=关闭”。
如果有帮助,下面是创建元框的代码:
// Add the Top and Featured News Meta Boxes ----------------------------------------//
add_action( \'add_meta_boxes\', \'ebrd_add_post_metaboxes\' );
function ebrd_add_post_metaboxes() {
add_meta_box(\'ebrd_feat\', \'Post status\', \'ebrd_feat\', \'ebrd_videos\', \'side\', \'default\');
}
// Featured Metabox
function ebrd_feat(){
global $post;
$custom = get_post_custom($post->ID);
if(isset($custom["ebrd_home_feat"][0])) $ebrd_home_feat = $custom["ebrd_home_feat"][0];
else $ebrd_home_feat = \'off\';
// We\'ll use this nonce field later on when saving.
wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>
<input type="checkbox" name="ebrd_home_feat" <?php if( $ebrd_home_feat != \'off\' ) { ?>checked="checked"<?php } ?> /> Home feature?
<?php
}
add_action(\'save_post\', \'save_details\');
function save_details($post_ID = 0) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
// if our nonce isn\'t there, or we can\'t verify it, bail
if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
// if our current user can\'t edit this post, bail
if( !current_user_can( \'edit_post\' ) ) return;
if(isset($_POST["ebrd_home_feat"])) $ebrd_home_feat = $_POST["ebrd_home_feat"];
else $ebrd_home_feat = \'off\';
if ($post_type) {
update_post_meta($post_ID, "ebrd_home_feat", $ebrd_home_feat);
}
return $post_ID;
}