首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress:自动填充后缩略图

Wordpress:自动填充后缩略图
EN

Stack Overflow用户
提问于 2011-12-15 18:48:46
回答 1查看 67关注 0票数 0

是否有合适的方式自动填充后缩略图?事实上,我向Vimeo-Api要了一个视频缩略图。此任务已完成...

但现在我需要将接收到的缩略图添加为Post-Thumbnail。这样也会生成所有相关的图像大小。

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-15 19:03:42

你可以试试这个(我没有测试它,但它应该可以工作,我在我的一个插件中使用了类似的东西):

代码语言:javascript
复制
            $image_path = 'path to your image';
            $id = 'id of corresponding post';
            $title = 'title of your image';

            // Checking the filetype
            $wp_filetype = wp_check_filetype(basename($image_path), null );

            $attachment = array(

               'post_mime_type' => $wp_filetype['type'],
               'post_title' => $title,
               'post_content' => '',
               'post_status' => 'inherit',
               'post_parent' => $id

            );

            // inserting the attachment
            $attach_id=wp_insert_attachment($attachment,$image_path);

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

            // Generating the metadate (and the thumbnails)
            $attach_data=wp_generate_attachment_metadata($attach_id,$image_path );

            // Setting it as thumbnail        
            update_post_meta( $id, '_thumbnail_id', $attach_id );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8518863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档