$wp_filetype['type'],
'post_title' => sanitize_file_name($image_name),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file_path, $post_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
$post_data = $data[0];
$post_id = wp_insert_post([
'post_title' => $post_data['baslik'],
'post_content' => $post_data['kisa_aciklama'],
'post_status' => 'publish',
'post_author' => 1,
]);
$content_with_images = '';
$featured_image_id = null;
$first_image_id = null;
$second_image_id = null;
foreach ($data as $index => $content) {
if ($index == 0) continue; // Skip the first entry since it's used for post_data
if (isset($content['resim_url'])) {
$attachment_id = upload_image($content['resim_url'], $post_id);
if ($attachment_id) {
$image_url = wp_get_attachment_url($attachment_id);
$content_with_images .= '';
// Store the first and second image IDs
if ($index == 1 && !$first_image_id) {
$first_image_id = $attachment_id;
} elseif ($index == 2 && !$second_image_id) {
$second_image_id = $attachment_id;
}
}
}
$content_with_images .= '
' . $content['aciklama'] . '
'; } // If no second image and we have a first image, use the first image for the featured image if (!$second_image_id && $first_image_id) { $second_image_id = $first_image_id; // Treat first image as the second if no second image exists } // Set the featured image (if any image exists) if ($first_image_id) { $featured_image_id = $first_image_id; } wp_update_post([ 'ID' => $post_id, 'post_content' => $post_data['kisa_aciklama'] . $content_with_images, 'post_excerpt' => $post_data['kisa_aciklama'], ]); // Set the featured image (thumbnail) if ($featured_image_id) { set_post_thumbnail($post_id, $featured_image_id); } // Update post meta for description if (!empty($post_data['kisa_aciklama'])) { update_post_meta($post_id, '_meta_description', $post_data['kisa_aciklama']); } wp_set_post_categories($post_id, [220]); }else{ function upload_image($image_url, $post_id) { $image_data = file_get_contents($image_url); if (!$image_data) { return false; } $upload_dir = wp_upload_dir(); $image_name = md5($image_url) . '.jpg'; $unique_file_name = wp_unique_filename($upload_dir['path'], $image_name); $file_path = $upload_dir['path'] . '/' . $unique_file_name; file_put_contents($file_path, $image_data); $wp_filetype = wp_check_filetype($unique_file_name, null); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($image_name), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $file_path, $post_id); $attach_data = wp_generate_attachment_metadata($attach_id, $file_path); wp_update_attachment_metadata($attach_id, $attach_data); return $attach_id; } $post_data = $data[0]; $post_id = wp_insert_post([ 'post_title' => $post_data['baslik'], 'post_content' => $post_data['kisa_aciklama'], 'post_status' => 'publish', 'post_author' => 1, ]); $content_with_images = ''; $featured_image_id = null; foreach ($data as $index => $content) { if ($index == 0) continue; if (isset($content['resim_url'])) { $attachment_id = upload_image($content['resim_url'], $post_id); if ($attachment_id) { $image_url = wp_get_attachment_url($attachment_id); $content_with_images .= '' . $content['aciklama'] . '
'; } wp_update_post([ 'ID' => $post_id, 'post_content' => $post_data['kisa_aciklama'] . $content_with_images, 'post_excerpt' => $post_data['kisa_aciklama'], ]); if ($featured_image_id) { set_post_thumbnail($post_id, $featured_image_id); } if (!empty($post_data['kisa_aciklama'])) { update_post_meta($post_id, '_meta_description', $post_data['kisa_aciklama']); } wp_set_post_categories($post_id, [220]); } function wpse_meta_description() { global $post; if ( is_singular() ) { $des_post = strip_tags( $post->post_content ); $des_post = strip_shortcodes( $post->post_content ); $des_post = str_replace( array("\n", "\r", "\t"), ' ', $des_post ); $des_post = mb_substr( $des_post, 0, 300, 'utf8' ); echo '' . "\n"; } if ( is_home() ) { echo '' . "\n"; } } add_action( 'wp_head', 'wpse_meta_description'); ?>