我正在将一张图片从youtube旁加载到wordpress中。它工作正常,代码的关键部分是:
// sideload it into wordpress (generates various sizes)
$thumbid = media_handle_sideload( $file_array, $post->ID );
if ( is_wp_error($thumbid) ) { // deal with error here }
// kill existing attachment first, if any
wp_delete_attachment(get_post_thumbnail_id($post->ID), true);
// and finally associate post with new thumbnail
set_post_thumbnail( $post, $thumbid );
这就是问题所在。我已向注册了5种图像大小
add_image_size
在我的Wordpress函数文件中。当我按照上面的代码进行侧加载时,WP生成所有大小的图像的所有拇指,除了一个,640x400。我认为它跳过了这个拇指,因为640x400实际上比从YouTube返回的图像大。然而,无论如何,我都需要得到那个缩略图——我想强制Wordpress生成那个缩略图。我该怎么做?
我像其他人一样注册图像大小:
add_image_size( \'static-head\', 640, 400, true );
所以我在寻找类似的函数
wp_generate_thumbnail($thumbid, \'static-head\')
, 这将在上载文件夹中保留正确的640x400图像。
谢谢你--
SO网友:Christopher Carvache
您必须使用wp\\u generate\\u attachment\\u metadata和wp\\u update\\u attachment\\u metadata来执行此操作。
// sideload it into wordpress (generates various sizes)
$thumbid = media_handle_sideload( $file_array, $post->ID );
if ( is_wp_error($thumbid) ) { // deal with error here }
// If this function is undefined in the environment where it is to be used,
// such as within a Shortcode, use the include function:
include( ABSPATH . \'wp-admin/includes/image.php\' );
// create the thumbnails
$attach_data = wp_generate_attachment_metadata( $thumbid, $file_array[\'tmp_name\'] );
wp_update_attachment_metadata( $thumbid, $attach_data );
// kill existing attachment first, if any
wp_delete_attachment(get_post_thumbnail_id($post->ID), true);
// and finally associate post with new thumbnail
set_post_thumbnail( $post, $thumbid );
wp\\u generate\\u attachment\\u元数据创建缩略图。wp\\u update\\u attachment\\u元数据确保它们实际上是媒体对象的一部分