我已经为我正在处理的主题创建了一个元框,它是一个简单的元框,带有一个用于特色帖子的复选框。问题是用户可以勾选该复选框multiple posts 在same category.
我想禁用旧帖子的元值,一旦在该类别下发布了新帖子。因此,基本上只允许检查任何给定类别上的1篇文章。我想我应该做点什么
delete_post_meta( $post_id, \'featured_post\' );
但我不知道如何检查以前的帖子并从中删除帖子元。有什么建议吗?
Update #1 :
这是我的元框:
//FEATURED POST CHECK BOX
//ADD THE META BOX
add_action( \'add_meta_boxes\', \'add_featured_slide\' );
function add_featured_slide(){
//POST TYPES TO HAVE THE CUSTOM META BOX
$ctptypes = array( \'post\', \'page\', \'your_custom_post_type\' );
foreach ( $ctptypes as $ctptype ) {
add_meta_box( \'featured-slide\', \'Featured Post\', \'featured_slide_func\', $ctptype, \'side\', \'high\' );
}
}
//DEFINE THE META BOX
function featured_slide_func( $post ){
$values = get_post_custom( $post->ID );
$check = isset( $values[\'special_box_check\'] ) ? esc_attr( $values[\'special_box_check\'][0] ) : \'\';
wp_nonce_field( \'my_featured_slide_nonce\', \'featured_slide_nonce\' );
?>
<p>
<input type="checkbox" name="special_box_check" id="special_box_check" <?php checked( $check, \'on\' ); ?> />
<label for="special_box_check">Feature this post?</label>
</p>
<?php
}
//SAVE THE META BOX DATA WITH THE POST
add_action( \'save_post\', \'featured_slide_save\' );
function featured_slide_save( $post_id ){
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST[\'featured_slide_nonce\'] ) || !wp_verify_nonce( $_POST[\'featured_slide_nonce\'], \'my_featured_slide_nonce\' ) ) return;
if( !current_user_can( \'edit_post\' ) ) return;
$allowed = array(
\'a\' => array(
\'href\' => array()
)
);
// IF CHECKED SAVE THE CUSTOM META
if ( isset( $_POST[\'special_box_check\'] ) && $_POST[\'special_box_check\'] ) {
add_post_meta( $post_id, \'special_box_check\', \'on\', true );
}
// IF UNCHECKED DELETE THE CUSTOM META
else {
delete_post_meta( $post_id, \'special_box_check\' );
}
}
SO网友:s_ha_dum
您声称希望“.允许在任何给定类别中选中1篇文章作为特色。”为此,您需要将您的special_box_check
使用类别,而不是简单的开/关切换。这需要改变形式$check
已设置,并且checked()
已使用。它还需要更改数据的保存方式。在下面的源代码中查找“后续更改”。
//ADD THE META BOX
add_action( \'add_meta_boxes\', \'add_featured_slide\' );
function add_featured_slide(){
//POST TYPES TO HAVE THE CUSTOM META BOX
$ctptypes = array( \'post\', \'page\', \'your_custom_post_type\' );
foreach ( $ctptypes as $ctptype ) {
add_meta_box( \'featured-slide\', \'Featured Post\', \'featured_slide_func\', $ctptype, \'side\', \'high\' );
}
}
//DEFINE THE META BOX
function featured_slide_func( $post ){
$values = get_post_custom( $post->ID );
$check = isset( $values[\'special_box_check\'] ) ? true : false;
wp_nonce_field( \'my_featured_slide_nonce\', \'featured_slide_nonce\' );
?>
<p>
<input type="checkbox" name="special_box_check" id="special_box_check" <?php checked( $check); ?> />
<label for="special_box_check">Feature this post?</label>
</p>
<?php
}
//SAVE THE META BOX DATA WITH THE POST
add_action( \'save_post\', \'featured_slide_save\', 10, 2 );
function featured_slide_save( $post_id, $post ){
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST[\'featured_slide_nonce\'] ) || !wp_verify_nonce( $_POST[\'featured_slide_nonce\'], \'my_featured_slide_nonce\' ) ) return;
if( !current_user_can( \'edit_post\' ) ) return;
$allowed = array(
\'a\' => array(
\'href\' => array()
)
);
// Changes follow
if ( isset( $_POST[\'special_box_check\'] ) && $_POST[\'special_box_check\'] ) {
$cats = wp_get_post_categories($post_id);
foreach ($cats as $v) {
delete_metadata ( \'post\', null, \'special_box_check\', $v, true );
}
foreach ($cats as $v) {
add_post_meta( $post_id, \'special_box_check\', $v);
}
} else {
delete_post_meta( $post_id, \'special_box_check\' );
}
}