小编Asp*_*ilt的帖子

使用 wp_insert_post 设置特色图像

// Auto post ( Unique File Date ).
$postData = array(
    'post_category' => array( $Category ),
    'post_status' => $Post_Status,
    'post_type' => $Post_Type
);
$post_id = wp_insert_post( $postData );

$getImageFile = 'http://localhost/Multisite/test2/wp-content/uploads/sites/4/Auto Post/twitter.png';

$attach_id = wp_insert_attachment( $postData, $getImageFile, $post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );

$attach_data = wp_generate_attachment_metadata( $attach_id, $getImageFile );

wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $post_id, $attach_id );
Run Code Online (Sandbox Code Playgroud)

上面的代码成功发布了帖子,但没有设置帖子特色图像。我不知道我在这里做错了什么。

php wordpress

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

php ×1

wordpress ×1