更新上传图片的metabox时出现问题

时间:2015-03-29 作者:Carl Willis

我创建了一个自定义帖子类型和元框,用于上载另一个图像。问题是,当我添加图像时,它会正确显示在后端的位置,当我更新它时,它也会显示相同的图像。

但是当我回到帖子修改它时,我找不到图片,如果我更新帖子,那么就没有图片显示。

<?php   
function hotels_meta_boxes() {
add_meta_box( \'hotels_form\', \'Hotels - Détails\', \'hotels_form\', \'hotels\', \'normal\', \'high\' );} function hotels_form() {
$post_id = get_the_ID();
$hotel_data = get_post_meta( $post_id, \'_hotel\', true );
$carte = ( empty( $hotel_data[\'carte\'] ) ) ? \'\' : $hotel_data[\'carte\'];
wp_nonce_field( \'hotels\', \'hotels\' );
?>  
    <h2 class="metaboxtitle">Coordonnées</h2>
<?php 
    global $post;

    $image_src = \'\';

    $image_id = get_post_meta( $post->ID, \'_hotel\', true );
    $image_src = wp_get_attachment_url( $image_id );

    ?>
    <img id="book_image" src="<?php echo $image_src ?>" style="max-width:100%;" />
    <input type="hidden" name="hotel[carte]" id="upload_image_id" value="<?php echo $image_id; ?>" />

        <a title="<?php esc_attr_e( \'Set book image\' ) ?>" href="#" id="set-book-image"><?php _e( \'Set book image\' ) ?></a>
        <a title="<?php esc_attr_e( \'Remove book image\' ) ?>" href="#" id="remove-book-image" style="<?php echo ( ! $image_id ? \'display:none;\' : \'\' ); ?>"><?php _e( \'Remove book image\' ) ?></a>
    </p>

    <script type="text/javascript">
    jQuery(document).ready(function($) {

        // save the send_to_editor handler function
        window.send_to_editor_default = window.send_to_editor;

        $(\'#set-book-image\').click(function(){

            // replace the default send_to_editor handler function with our own
            window.send_to_editor = window.attach_image;
            tb_show(\'\', \'media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=true\');

            return false;
        });

        $(\'#remove-book-image\').click(function() {

            $(\'#upload_image_id\').val(\'\');
            $(\'img\').attr(\'src\', \'\');
            $(this).hide();

            return false;
        });

        // handler function which is invoked after the user selects an image from the gallery popup.
        // this function displays the image and sets the id so it can be persisted to the post meta
        window.attach_image = function(html) {

            // turn the returned image html into a hidden image element so we can easily pull the relevant attributes we need
            $(\'body\').append(\'<div id="temp_image">\' + html + \'</div>\');

            var img = $(\'#temp_image\').find(\'img\');

            imgurl   = img.attr(\'src\');
            imgclass = img.attr(\'class\');
            imgid    = parseInt(imgclass.replace(/\\D/g, \'\'), 10);

            $(\'#upload_image_id\').val(imgid);
            $(\'#remove-book-image\').show();

            $(\'img#book_image\').attr(\'src\', imgurl);
            try{tb_remove();}catch(e){};
            $(\'#temp_image\').remove();

            // restore the send_to_editor handler function
            window.send_to_editor = window.send_to_editor_default;

        }

    });
    </script>

</p>
<?php } add_action( \'save_post\', \'hotels_save_post\' );/*** Data validation and saving** This functions is attached to the \'save_post\' action hook.*/ function hotels_save_post( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
    return;

if ( ! empty( $_POST[\'hotels\'] ) && ! wp_verify_nonce( $_POST[\'hotels\'], \'hotels\' ) )
    return;

if ( ! empty( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) )
        return;
} else {
    if ( ! current_user_can( \'edit_post\', $post_id ) )
        return;
}

if ( ! wp_is_post_revision( $post_id ) && \'hotels\' == get_post_type( $post_id ) ) {
    remove_action( \'save_post\', \'hotels_save_post\' );

    wp_update_post( array(
        \'ID\' => $post_id,
    ) );

    add_action( \'save_post\', \'hotels_save_post\' );
}

if ( ! empty( $_POST[\'hotel\'] ) ) {
    $hotel_data[\'carte\'] = ( empty( $_POST[\'hotel\'][\'carte\'] ) ) ? \'\' : sanitize_text_field( $_POST[\'hotel\'][\'carte\'] );
    update_post_meta( $post_id, \'_hotel\', $hotel_data );
} else {
    delete_post_meta( $post_id, \'_hotel\' );
}} add_filter( \'manage_edit-hotels_columns\', \'hotels_edit_columns\' );/******************** and to show it i use *************************/if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
        $post_id = get_the_ID();
        $hotel_data = get_post_meta( $post_id, \'_hotel\', true );
        $carte = ( empty( $hotel_data[\'carte\'] ) ) ? \'\' : $hotel_data[\'carte\'];
        $hotels .= \'<div class="hotel_map">\';
                $carte = ( empty( $hotel_data[\'carte\'] ) ) ? \'Map introuvable\' : $hotel_data[\'carte\'];
            $hotels .= \'<img src="\' . wp_get_attachment_url( $carte ) . \'" style="margin-top: 10px;"/>\' ;
            $hotels .= \'</div>\';//fin map
            endwhile;
    wp_reset_postdata();
}
else {
    echo \'Pas d\\\'hotels pour l\\\'instant\';
}
return $hotels;}

1 个回复
SO网友:Sebastian

我没有时间写你的代码,但在你的帮助下,我可以工作

function wms_add_headerimage_cb( $post ){
//campuri
$values = get_post_custom( $post->ID );

$headerpageimage        = isset( $values[\'headerpageimage\'][0] ) ? esc_attr( $values[\'headerpageimage\'][0] ) : \'\';
$img_src                = wp_get_attachment_image_src( $headerpageimage, \'medium\' );

wp_nonce_field( \'allpages_meta_box\', \'allpages_meta_box_nonce\' );
?>
    <p>
        <img id="headerpage_image" src="<?php echo $img_src[0] ?>" style="max-width:100%;" />
        <input type="hidden" name="headerpageimage" id="headerpageimage" value="<?php echo $headerpageimage; ?>" />

    <a title="<?php esc_attr_e( \'Set image\' ) ?>" href="#" id="set-book-image"><?php _e( \'Set image\' ) ?></a>
    <a title="<?php esc_attr_e( \'Remove image\' ) ?>" href="#" id="remove-headerpageimage" style="<?php echo ( ! $headerpageimage ? \'display:none;\' : \'\' ); ?>"><?php _e( \'Remove image\' ) ?></a>
</p>
<script type="text/javascript">
jQuery(document).ready(function($) {

    // save the send_to_editor handler function
    window.send_to_editor_default = window.send_to_editor;

    $(\'#set-book-image\').click(function(){

        // replace the default send_to_editor handler function with our own
        window.send_to_editor = window.attach_image;
        tb_show(\'\', \'media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=true\');

        return false;
    });

    $(\'#remove-headerpageimage\').click(function() {

        $(\'#headerpageimage\').val(\'\');
        $(\'img\').attr(\'src\', \'\');
        $(this).hide();

        return false;
    });

    // handler function which is invoked after the user selects an image from the gallery popup.
    // this function displays the image and sets the id so it can be persisted to the post meta
    window.attach_image = function(html) {

        // turn the returned image html into a hidden image element so we can easily pull the relevant attributes we need
        $(\'body\').append(\'<div id="temp_image">\' + html + \'</div>\');

        var img = $(\'#temp_image\').find(\'img\');

        imgurl   = img.attr(\'src\');
        imgclass = img.attr(\'class\');
        imgid    = parseInt(imgclass.replace(/\\D/g, \'\'), 10);

        $(\'#headerpageimage\').val(imgid);
        $(\'#remove-headerpageimage\').show();

        $(\'img#headerpage_image\').attr(\'src\', imgurl);
        try{tb_remove();}catch(e){};
        $(\'#temp_image\').remove();

        // restore the send_to_editor handler function
        window.send_to_editor = window.send_to_editor_default;

    }

});
</script>
<?php
}

添加操作(“save\\u post”、“headerimage\\u save”);函数headerimage\\u save($post\\u id){

if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return $post_id;
if( !isset( $_POST[\'allpages_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'allpages_meta_box_nonce\'], \'allpages_meta_box\' ) ) return $post_id;    
if( !current_user_can( \'edit_post\' ) ) return $post_id;  
$allowed = array(   
    \'a\' => array( // on allow a tags  
        \'href\' => array() // and those anchors can only have href attribute  
    )  
);

// quantity Icon
if( isset( $_POST[\'headerpageimage\'] ) )  update_post_meta( $post_id, \'headerpageimage\', esc_attr( $_POST[\'headerpageimage\']) );
}

结束

相关推荐

管理面板-禁用将所选术语移动到Metabox顶部

默认情况下,无论何时选择一个类别,它都会将其移动到术语列表的顶部,这在大多数情况下都非常方便用户使用。我想禁用此功能,并将我的术语列表保留为默认顺序,无论选择什么。我之所以要这样做,是因为我有一个依赖于<ul class=\"children\"> ,但如果选择了某个术语,则会将其从该格式中删除并置于列表顶部。我四处搜索过,但没有使用正确的关键字来提取任何有用的内容。我肯定我需要做点什么Walker_Category_Checklist 但步行者对我来说是个谜。我将尝试使用它和一些WordPr