2014-02-10 103 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); 

谢谢。

相关问题