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))
谢谢。
嗨,我现在遇到这个问题,你有没有找到解决办法? – brndnmg
@brndnmg请检查我的答案我把答案放在我的问题上。此功能正常工作。 –