这可能是一个有点基本的问题,但我有点迷路了。基本上,我试图在自定义的帖子类型中创建一个复选框元状态。然后我想检查该复选框是否已被选中以更改主题中显示的内容。
这就是我所拥有的:
我的自定义帖子类:
function slide_box() {
$slide_stored_meta = get_post_meta((int)$_REQUEST['post'] );
?>
<div>
<label for="slide-checkbox">
<input type="checkbox" name="slide-checkbox" id="slide-checkbox" value="yes" <?php if ( isset ( $slide_stored_meta['slide-checkbox'] ) ) checked( $slide_stored_meta['slide-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Display Title and Excerpt?', 'prfx-textdomain' )?>
</label>
</div>
<?php
}
add_action('save_post','slide_save_meta');
function slide_save_meta($postID) {
if ( is_admin() ) {
// Checks for input and saves
if( isset( $_POST[ 'slide-checkbox' ] ) ) {
update_post_meta( $postID, 'slide-checkbox', 'yes' );
} else {
update_post_meta( $postID, 'slide-checkbox', 'no' );
}
}
}
?>
它似乎保存得很好,因为当我更新帖子时,它会正确地保持选中状态。
下一个我的主题模板文件:
<?php $slider = new WP_Query(array('post_type' => 'slide', 'posts_per_page'=>20, 'suppress_filters'=>0 )); ?>
<?php if ($slider->have_posts()) : while($slider->have_posts()) : $slider->the_post(); ?>
**<?php if(get_post_meta($post->ID), 'slide-checkbox', true) { ?>**
为什么使用零索引?尝试直接变量:
checked( $slide_stored_meta['slide-checkbox'], "yes")