2014-02-10 104 views
0

你好朋友,我曾經設置發佈縮略圖programetically。Wordpress wp_update_attachment_metadata圖片寬度和高度= 1?

這是我的代碼。

foreach ($csv as $key => $value) { 
    $filename = "wp-content/uploads/images/".$value[8].".jpg"; 
    $wp_filetype = wp_check_filetype(basename($filename), null); 
    $attachment = array(
    'post_mime_type' => $wp_filetype['type'], 
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 
    'post_content' => 'this is the first project file.', 
    'post_status' => 'Published' 
    ); 
    $my_post = array(
     'post_title' => $value[0], 
     'post_content' => $value[2], 
     'post_status' => 'publish', 
     'post_author' => 1, 
     'post_type' => 'post_staff' 
    ); 
    $post_id = wp_insert_post($my_post); 

    $attach_id = wp_insert_attachment($attachment, $filename,$post_id); 
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
    wp_update_attachment_metadata($attach_id, $attach_data);  


    add_post_meta($post_id, '_thumbnail_id', $attach_id); 
    update_post_meta($post_id, '_staff_name', $value[1]);  
    update_post_meta($post_id, '_staff_city', $value[3]);  
    update_post_meta($post_id, '_staff_postal_code', $value[4]);  
    update_post_meta($post_id, '_staff_direct_line', $value[5]);  
    update_post_meta($post_id, '_staff_fax', $value[6]);  
    update_post_meta($post_id, '_staff_email', $value[7]);  
    $tagd = array(9); 
    wp_set_post_terms($post_id, $tagd, 'department'); 
    if($value[3] == "St. John's, NL"){ 
     $tagl = array(8); 
    }else if($value[3] == "Corner Brook"){ 
     $tagl = array(7); 
    } 
    wp_set_post_terms($post_id, $tagl, 'location'); 
    if(set_post_thumbnail($post_id, $attach_id)){ 
     echo "image set"; 
    } 
} 

這是工作正常,但與大小1×1 WIDTH = 1和高度的進口特徵圖像= 1

爲什麼需要寬度和高度爲1 autometically請幫助。

當我試圖使用get_the_post_thumbnail獲取圖像的返回圖像。

找到圖像,但默認情況下圖像寬度= 1,高度= 1。

這是我的代碼。

get_the_post_thumbnail(get_the_ID(), array(250,165)) 

謝謝。

+0

嗨,我現在遇到這個問題,你有沒有找到解決辦法? – brndnmg

+0

@brndnmg請檢查我的答案我把答案放在我的問題上。此功能正常工作。 –

回答

0

此函數用於在wordpress中導入包含圖片的文章。

function fetch_media($file_url, $post_id) { 
require_once(ABSPATH . 'wp-load.php'); 
require_once(ABSPATH . 'wp-admin/includes/image.php'); 
global $wpdb; 

if(!$post_id) { 
    return false; 
} 

//directory to import to  
$artDir = 'wp-content/uploads/importedmedia/'; 

//if the directory doesn't exist, create it 
if(!file_exists(ABSPATH.$artDir)) { 
    mkdir(ABSPATH.$artDir); 
} 

//rename the file... alternatively, you could explode on "/" and keep the original file name 
$extpop = explode(".", $file_url); 
$ext = array_pop($extpop); 

$new_filename = 'blogmedia-'.$post_id.".".$ext; //if your post has multiple files, you may need to add a random number to the file name to prevent overwrites 

if (@fclose(@fopen($file_url, "r"))) { //make sure the file actually exists 
    copy($file_url, ABSPATH.$artDir.$new_filename); 

    $siteurl = get_option('siteurl'); 
    $file_info = getimagesize(ABSPATH.$artDir.$new_filename); 

    //create an array of attachment data to insert into wp_posts table 
    $artdata = array(); 
    $artdata = array(
     'post_author' => 1, 
     'post_date' => current_time('mysql'), 
     'post_date_gmt' => current_time('mysql'), 
     'post_title' => $new_filename, 
     'post_status' => 'inherit', 
     'comment_status' => 'closed', 
     'ping_status' => 'closed', 
     'post_name' => sanitize_title_with_dashes(str_replace("_", "-", $new_filename)),           'post_modified' => current_time('mysql'), 
     'post_modified_gmt' => current_time('mysql'), 
     'post_parent' => $post_id, 
     'post_type' => 'attachment', 
     'guid' => $siteurl.'/'.$artDir.$new_filename, 
     'post_mime_type' => $file_info['mime'], 
     'post_excerpt' => '', 
     'post_content' => '' 
    ); 

    $uploads = wp_upload_dir(); 
    $save_path = $uploads['basedir'].'/importedmedia/'.$new_filename; 

    //insert the database record 
    $attach_id = wp_insert_attachment($artdata, $save_path, $post_id); 

    //generate metadata and thumbnails 
    if ($attach_data = wp_generate_attachment_metadata($attach_id, $save_path)) { 
     wp_update_attachment_metadata($attach_id, $attach_data); 
    } 

    //optional make it the featured image of the post it's attached to 
    $rows_affected = $wpdb->insert($wpdb->prefix.'postmeta', array('post_id' => $post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $attach_id)); 
} 
else { 
    return false; 
} 

return true; 
} 

將此值傳遞給此函數。

$file_name = 'full path of existing image'; 
$post_id = '1'; 
fetch_media($filename,$post_id); 

謝謝。

相關問題