我想将图片设置为帖子的精选图片。我在wordpress文档中找到了这段代码,它将图像保存在上传目录中,但图像不再被设置为帖子的精选图像(代码中为37)。如何在Wordpress上用php设置图像为特色图像?
你能看一下吗?非常感谢
<?php
$wp_filetype = wp_check_filetype(basename($filename), null);
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, 37);
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
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);
?>
感谢,这是我正在寻找的功能:) – JojoLapin45