• не добавляется миниатюра к посту, хотя сам пост создается

                             $my_post = array(
    				 'post_title' => $fam.' '.$name.' '.$surname,
    				 'post_content' => 'Я использую карту скидок "'.$postcont.'"',
    				 'post_status' => 'publish',
    				 'post_author' => $user->ID,
    				 'post_category' => array(108)
    			  );
    				$post_id = wp_insert_post($my_post);
    
    $image_url        = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // Define the image URL here
    $image_name       = 'wp-header-logo.png';
    $upload_dir       = wp_upload_dir(); // Set upload folder
    $image_data       = file_get_contents($image_url); // Get image data
    $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); 
    $filename         = basename( $unique_file_name ); // Create image file name
    if( wp_mkdir_p( $upload_dir['path'] ) ) {
    $file = $upload_dir['path'] . '/' . $filename;
    } else {
    $file = $upload_dir['basedir'] . '/' . $filename;
    }
    file_put_contents( $file, $image_data );
    $wp_filetype = wp_check_filetype( $filename, null );
    
    // Set attachment data
    $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title'     => sanitize_file_name( $filename ),
    '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 );

    В чем мб проблема???

    • Тема изменена 8 лет, 5 месяцев назад пользователем hasnike.
Просмотр 1 ответа (всего 1)
Просмотр 1 ответа (всего 1)
  • Тема «не добавляется миниатюра» закрыта для новых ответов.