Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上传wordpress后设置特色图片_Php_Wordpress_Custom Post Type_Dynamic Featured Image - Fatal编程技术网

Php 上传wordpress后设置特色图片

Php 上传wordpress后设置特色图片,php,wordpress,custom-post-type,dynamic-featured-image,Php,Wordpress,Custom Post Type,Dynamic Featured Image,我有这个代码,它可以很好地将上传的照片插入wordpress库中,并成功地创建了帖子 但将上传的照片设置为帖子上的特色图片是行不通的 注意:我使用自定义帖子类型 我在stackoverflow网络上尝试了很多解决方案,但都没有成功 $dir = plugin_dir_path( __FILE__ ); $file_path = $dir."/uploads/"; $text = $_POST['text']; $user = $_POST['usr']; $file

我有这个代码,它可以很好地将上传的照片插入wordpress库中,并成功地创建了帖子

但将上传的照片设置为帖子上的特色图片是行不通的

注意:我使用自定义帖子类型

我在stackoverflow网络上尝试了很多解决方案,但都没有成功

$dir = plugin_dir_path( __FILE__ );
   $file_path = $dir."/uploads/";
    $text = $_POST['text'];
    $user = $_POST['usr'];
    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path);


$file = $file_path;
$filename = basename($file);

$upload_file = wp_upload_bits($filename, null, file_get_contents($file));
if (!$upload_file['error']) {
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_parent' => $parent_post_id,
        'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $parent_post_id );
    if (!is_wp_error($attachment_id)) {
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
        wp_update_attachment_metadata( $attachment_id,  $attachment_data );
    }
}

unlink($file_path);

$user = get_userdatabylogin($_POST['usr']);
$user_ID = $user->ID; // prints the id of the user

global $user_ID;

$new_post = array(
'post_title' => 'My New Post',
'post_content' => $_POST['text'],
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'taken_photos',
'post_category' => array(0)
);
$pid = wp_insert_post($new_post);

  update_post_meta($pid, '_thumbnail_id', $attachment_id);

  $attachment_data = array(
    'ID' => $attachment_id,
    'post_excerpt' => 'TITLE'
  );
你能把文件路径设置成那样吗?希望它能为你工作。

在你的线路之后

wp_update_attachment_metadata( $attachment_id,  $attachment_data );
添加附加行

add_post_meta($parent_post_id, '_thumbnail_id', $attachment_id);
$file_路径与此无关,而且此文件路径是临时的,如果您仔细查看代码,您将看到在将此文件插入wordpress媒体库后,我将删除此文件
add_post_meta($parent_post_id, '_thumbnail_id', $attachment_id);