简体   繁体   English

如何在WordPress中插入设置特色图片?

[英]How to insert Set featured image in WordPress?

Insert post in database works correct.Uploading file in wordpress uploads folder works correct also.The problem is I can't set this file in admin set featured image section. 在数据库中插入帖子是正确的.wordpress上载文件夹中的上传文件也正确。问题是我无法在管理集特色图片部分中设置此文件。 I need to upload this file with my function crb_insert_email_as_post . 我需要使用函数crb_insert_email_as_post上传此文件。 I don't know when I'm wrong. 我不知道什么时候错了。 Can someone help? 有人可以帮忙吗? This is my function for updating wordpress database 这是我更新Wordpress数据库的功能

function crb_insert_email_as_post( $email ) {
$post_attr = [
    'post_type'    => 'crb_form_submission',
    'post_title'   => $email->crb_get_subject(),
    'post_content' => $email->crb_get_message(),
    'post_status'  => 'publish',
];

$post_id = wp_insert_post( $post_attr );

add_post_meta( $post_id, '_crb_sender', $email->crb_get_sender() );
add_post_meta( $post_id, '_crb_sender_email', $email->crb_get_sender_email() 
);

$attachment = array(
    'post_mime_type' => $email->crb_get_file_type(),
    'post_parent' => $post_id,
    'post_title' => '',
    'post_content' => '',
    'post_status' => 'inherit'
);

$attachment_id = wp_insert_attachment( $attachment, $email-
>crb_get_file_path(), $post_id );

require_once( ABSPATH . 'wp-admin/includes/image.php' );

$attach_data = wp_generate_attachment_metadata( $attachment_id, $email-
>crb_get_file_path() );

wp_update_attachment_metadata( $attachment_id ,  $attach_data );
}

And this is my Email class 这是我的电子邮件课程

class Email {
private $sender;
private $subject;
private $sender_email;
private $message;
private $file;

public function __construct( $sender, $subject, $sender_email, $message, $file = '' ) {
    $this->sender = $sender;
    $this->subject = $subject;
    $this->sender_email = $sender_email;
    $this->message =  $message;
    $this->file = $file;
}

public function crb_get_sender() {
    return $this->sender;
}

public function crb_get_subject() {
    return $this->subject;
}

public function crb_get_sender_email() {
    return $this->sender_email;
}

public function crb_get_message() {
    return $this->message;
}

public function crb_get_file() {
    return $this->file;
}

public function crb_get_file_type() {
    return $this->file['file']['type'];
}

function crb_get_file_path() {
    return wp_upload_dir()['path'] . '/' . $this->file['file']['name'];
}

public function crb_move_file_in_uploads() {
    if ( ! $this->file ) {
        return;
    }

    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    $movefile = wp_handle_upload( $this->file['file'], 'wp_handle_upload' );

    $uploadedfile = $this->file['file'];

    $upload_overrides = array( 'test_form' => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
}
}

I guess that you're missing the function for setting it as the featured image. 我猜想您缺少将其设置为特色图片的功能。

set_post_thumbnail( $post_id, $attachment_id );

after

wp_update_attachment_metadata( $attachment_id ,  $attach_data );

And you might need : 您可能需要:

require_once( ABSPATH . 'wp-admin/includes/post.php' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM