我只想添加自定义帖子、缩略图和自定义字段<这是我的代码,我正在尝试添加帖子、缩略图和一个自定义字段。但我不知道如何上传这篇特定帖子的缩略图。以及如何检索此帖子。
$image_url = $_FILES[\'post_img\'][\'name\'];
$tags = $_POST[\'post_tags\'];
$my_post = array(
//\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => wp_strip_all_tags( $_POST[\'post_title\'] ),
\'post_content\' => $_POST[\'post_content\'],
\'post_status\' => \'publish\',
\'post_type\' => \'Custamize_post\',
\'tags_input\' => array(\'thread_tag\' => $tags),
\'post_author\' => $user_ID
);
$post_link = $_POST[\'post_link\'];
add_post_meta($post_id, \'post_link\', $post_link, true);
我在一些文章中看到他们使用此代码上载文件。
$upload_dir = wp_upload_dir();
//$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir[\'path\']))
$file = $upload_dir[\'path\'] . \'/\' . $filename;
else
$file = $upload_dir[\'basedir\'] . \'/\' . $filename;
file_put_contents($file, $image_url);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => \'Custamize_post\',
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . \'wp-admin/includes/image.php\');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
但我这里主要关心的是如何将上面创建的帖子连接到这个附件。