繁体   English   中英

在WordPress中使用复选框发布缩略图

[英]Post thumbnail with checkbox in WordPress

我尝试在WordPress管理中选中一个复选框。 我想如果选中此复选框,则帖子缩略图为aperar。 但是我的代码不起作用。 问题是什么?

非常感谢。

我的复选框代码:

add_action('add_meta_boxes', 'add_ot_meta_box');
function add_ot_meta_box() {
    add_meta_box('ot-meta-box', esc_html__('Post status', 'post_status'), 'ot_meta_box_markup', 'page', 'side', 'high', null);
    add_meta_box('ot-meta-box', esc_html__('Post status', 'post_status'), 'ot_meta_box_markup', 'post', 'side', 'high', null);
}
//create the boxes html markup
function ot_meta_box_markup() {
    global $post;
    $checkbox_value = get_post_meta($post->ID, 'ot-meta-box-checkbox', true);
    wp_nonce_field(basename(__FILE__), 'ot-meta-box-nonce');
    ?>
        <div>
            <input id="ot-meta-box-checkbox" type="checkbox" name="ot-meta-box-checkbox" value="1" <?php checked( $checkbox_value, 1); ?>/>
            <label for="ot-meta-box-checkbox"><?php esc_html_e('Post status') ?></label>
        </div>
    <?php
}
// save checkbox meta
function save_ot_meta_box($post_id) {
    if (!isset($_POST['ot-meta-box-nonce']) || !wp_verify_nonce($_POST['ot-meta-box-nonce'], basename(__FILE__)))
        return $post_id;
    if(!current_user_can("edit_post", $post_id))
        return $post_id;
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    if( isset( $_POST['ot-meta-box-checkbox'] ) ){
        update_post_meta( $post_id, 'ot-meta-box-checkbox', true );
    } else{
        update_post_meta( $post_id, 'ot-meta-box-checkbox', false );
    }
}
add_action('save_post', 'save_ot_meta_box', 10, 3);

和我的回调代码单:

<?php $otoptions = get_option('ot-meta-box-checkbox');
if ( '' !== get_the_post_thumbnail() && ($otoptions['ot-meta-box-checkbox'] !== '1') && is_single() ) : ?>
    <div class="post-thumbnail">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail( 'my-featured-image' ); ?>
            </a>
        </div><!-- .post-thumbnail -->
    <?php endif; ?>

好的,我找到了:

<?php $checkbox_value = get_post_meta($post->ID, 'ot-meta-box-checkbox', true);
if ( '' !== get_the_post_thumbnail() && ($otoptions['ot-meta-box-checkbox'] !== '1') && is_single() ) : ?>
    <div class="post-thumbnail">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail( 'my-featured-image' ); ?>
            </a>
        </div><!-- .post-thumbnail -->
    <?php endif; ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM