【问题标题】:How to insert Set featured image in WordPress?如何在 WordPress 中插入设置特色图片?
【发布时间】:2018-03-15 18:04:36
【问题描述】:

在数据库中插入帖子正确。在 wordpress 上传文件夹中上传文件也正确。问题是我无法在管理员 设置特色图片部分设置此文件。 我需要用我的函数 crb_insert_email_as_post 上传这个文件。 我不知道我什么时候错了。 有人可以帮忙吗? 这是我更新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 );
}

这是我的电子邮件课程

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 );
}
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我猜您缺少将其设置为特色图像的功能。

    set_post_thumbnail( $post_id, $attachment_id );
    

    之后

    wp_update_attachment_metadata( $attachment_id ,  $attach_data );
    

    你可能需要:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多