使用元框自定义帖子类型图像上传

时间:2012-06-29 08:24:20

标签: wordpress custom-post-type

我无法在自定义帖子类型中找到我的附加图片。 functions.php中的代码

$prefix = 'custom_';
$custom_meta_fields = array(

    array(
        //'label'   => 'Textarea',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'textarea',
        'type'  => 'textarea'
    )
);

// Default metabox for custom post types.
function avz_custom_meta_box() {

   // $post_types = get_post_types( array( 'public' => true ) );
   $post_types = get_post_types();
    foreach ( $post_types as $post_type ) {
        if ( $post_type == 'page' || $post_type =='post' )
            continue;
        add_meta_box(
        $prefix.'image', 
        'Header Image Upload Box', 
        'show_avz_custom_meta_box', 
        $post_type, 
        'normal', 
        'high' );
    }

}
add_action('add_meta_boxes', 'avz_custom_meta_box');

function show_avz_custom_meta_box() {
    global $avz_custom_meta_box_fields, $post;
    // Use nonce for verification
    echo '<input type="hidden" name="avz_custom_meta_box_fields_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
    echo '<table class="form-table">';
    foreach ($avz_custom_meta_box_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field['id'], true);
        // begin a table row with
        echo '<tr>
                <td>';
                switch($field['type']) {
                    case 'image':
                        $image = get_template_directory_uri().'/images/image.png';  
                        echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
                        if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }               
                        echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
                                    <img src="'.$image.'" class="custom_preview_image" alt="" /><br />
                                        <input class="custom_upload_image_button button" type="button" value="Choose Image" />
                                        <small>&nbsp;<a href="#" class="custom_clear_image_button">Remove Image</a></small>
                                        <br clear="all" /><span class="description">'.$field['desc'].'</span>';
                    break;

                } //end switch
        echo '</td></tr>';
    } // end foreach
    echo '</table>'; // end table
}

// Save the Data
function save_multiBox_custom_meta($post_id) {
    global $avz_custom_meta_box_fields;

    // verify nonce
    if (!wp_verify_nonce($_POST['avz_custom_meta_box_fields_nonce'], basename(__FILE__))) 
        return $post_id;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id))
            return $post_id;
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
    }

    // loop through fields and save the data
    foreach ($avz_custom_meta_box_fields as $field) {
        if($field['type'] == 'tax_select') continue;
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    } // enf foreach

    // save taxonomies
    $post = get_post($post_id);
    $category = $_POST['category'];
    wp_set_object_terms( $post_id, $category, 'category' );
}
add_action('save_post', 'save_multiBox_custom_meta');

在single.php中

if( $image_upload_id = get_post_meta($post->ID, $field['custom_image'], true)){
    $img = $image_upload_id ['custom_image'][0];
    echo wp_get_attachment_image($img, 'full'); 
}

但找不到附图。 在管理员上传的图片显示但在帖子页面没有显示。

1 个答案:

答案 0 :(得分:2)

您是否尝试过访问帖子附件?只是为了看它在那里?

<?php 
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);

$attachments = get_posts($args);
if ($attachments) {
    echo "<pre>";
    print_r($attachments);
    echo "</pre>";
    $attachment = $attachments[0];
    // print full link to attachment
    the_attachment_link($attachment->ID, false);
}else{
    echo "No Attachments for this post!";
}
?>