我想將圖片設置爲帖子的精選圖片。我在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