我正在将其他网站的内容废弃到wordpress网站,我做了所有的事情,下面是我的插入查询
$my_post = array(
\'post_title\' => $my_title,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_category\' => array(5),
\'post_content\' => $res_final,
\'post_date\' => date(\'Y-m-d H:i:s\'),
\'post_date_gmt\' => date(\'Y-m-d H:i:s\'),
\'post_type\' => \'post\'
);
$post_id = wp_insert_post( $my_post );
我可以插入我的标题和内容,是的,它正在显示:),甚至我可以看到图像插入到文章中,因为图像与内容一起出现,但我计划将图像单独从内容中取出,并单独存储为帖子图像,以便它将出现在头版。
关于如何将图像添加到帖子中的任何帮助,以及插入我的图像URL的最佳方式,一个插入查询来完成此操作。请帮帮我
SO网友:Pontus Abrahamsson
阅读关于wp_insert_attachment(), wp_update_attachment_metadata() 和wp_generate_attachment_metadata()
要添加附件,请使用wp_insert_attachment()
示例:
<?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 );
?>