首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress wp-insert-post内容中的图像附件

Wordpress wp-insert-post内容中的图像附件
EN

Stack Overflow用户
提问于 2014-02-13 23:36:58
回答 1查看 1.4K关注 0票数 0

我正在为wordpress创建一个自定义的帖子提交表单,目前我可以创建帖子,并将图像上传并附加到帖子上,但下一步我正在努力-我希望上传的图像自动显示在帖子上,我查看了特色图像选项,但就是不能让它工作

代码语言:javascript
复制
$my_post = array();
$my_post['post_title'] = $newid;
$my_post['post_content'] = $imageurl1.$imageurl.$config_basedir.$uploaddir.$id . ".gif". $imageurl2;
$my_post['post_author'] = 1;
$my_post['post_status'] = draft;
$my_post['post_category'] = array($a);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
//
$wp_filetype = wp_check_filetype(basename($id . ".gif"), null );
$attachment = array();
$attachment['post_mime_type'] = $wp_filetype['type'];
$attachment['post_title'] = $newid;
$attachment['post_content'] = '';
$attachment['post_status'] = 'inherit';
$attach_id = wp_insert_attachment($attachment, $id . ".gif", $post_id)
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id,  $attach_data);
set_post_thumbnail($post_id, $attach_id);
//
wp_redirect( site_url()."?p=$post_id" ); exit();

任何帮助我们都将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2014-02-14 00:20:18

使用set_post_thumbnail。您可以传入post对象或ID,缩略图ID为媒体附件ID。

代码语言:javascript
复制
set_post_thumbnail( $post, $thumbnail_id );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21758674

复制
相关文章

相似问题

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