admin管理员组文章数量:1025492
I have uploaded some images to the wordpress media library. Now I am going to create some posts using php and want to use those already uploaded images as featured image for these posts respectively. How to set image as featured image for a post. I have tried below code. but it sets an empty image as featured image to posts.
function set_featured_image($post_id, $image_url){
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
$image_attachment_id = $attachment[0];
$attachment = array(
'ID' => $image_attachment_id,
'post_parent' => $post_id
);
$attachement_id = wp_insert_attachment( $attachment );
set_post_thumbnail( $post_id, $attachement_id ); // also tried using $image_attachment_id but same issue
I got this blank featured image
Prior thanks to any help.
I have uploaded some images to the wordpress media library. Now I am going to create some posts using php and want to use those already uploaded images as featured image for these posts respectively. How to set image as featured image for a post. I have tried below code. but it sets an empty image as featured image to posts.
function set_featured_image($post_id, $image_url){
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
$image_attachment_id = $attachment[0];
$attachment = array(
'ID' => $image_attachment_id,
'post_parent' => $post_id
);
$attachement_id = wp_insert_attachment( $attachment );
set_post_thumbnail( $post_id, $attachement_id ); // also tried using $image_attachment_id but same issue
I got this blank featured image
Prior thanks to any help.
版权声明:本文标题:php - How to programmatically set existing image in media library the featured image for a post in wordpress 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745625406a2159828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论