如何更改缩略图src属性?

时间:2016-05-27 作者:Kumar

我正在开发一个插件,可以从URL生成缩略图,从帖子中获取第一幅图像;它类似于自动缩略图生成器。我明白了img 标记,但URL已更改。这是我的代码:

$image_data = file_get_contents( $thumbnail );
$filename   = basename( $thumbnail );

file_put_contents( $thumbnail, $image_data );

$wp_filetype = wp_check_filetype( $filename, null );

$attachment  = array(
    \'post_mime_type\' => $wp_filetype[\'type\'],
    \'post_title\'     => sanitize_file_name( $filename ),
    \'post_content\'   => \'\',
    \'post_status\'    => \'inherit\'
);

$attach_id = wp_insert_attachment( $attachment, $thumbnail, $postId );

require_once( ABSPATH . \'wp-admin/includes/image.php\' );

$attach_data = wp_generate_attachment_metadata( $attach_id, $thumbnail );
$res1        = wp_update_attachment_metadata( $attach_id, $attach_data );

update_post_meta( $postId, \'_thumbnail_id\', $attach_id );
它给了我img 标记为:

 <img width="350" height="421" src="http://localhost/w/wp-content/uploads/https://1.bp.blogspot.com/-yL4P2_f5ygk/Vxx2SBGRrII/AAAAAAAANL0/pfK0uEWrRTwZuJoq0UuwxNHpXMQ4ouypQCLcB/s1600/Step%2BUp%2BRevolution%2B2012%2BDual%2BAudio%2BHindi%2BBluRay.jpg"  class="attachment-post-thumbnail size-post-thumbnail wp-post-image"  alt="Step2BUp2BRevolution2B20122BDual2BAudio2BHindi2BBluRay.jpg" />
如何删除http://localhost/w/wp-content/uploads/ 从URL?

1 个回复
SO网友:The Maniac

我在这里猜测,如果我错了,我会编辑/删除

检查$thumbnail 我打赌你会找到一个URI。

documentation:

使用绝对路径,而不是文件的URI。