php – WordPress – 以编程方式添加不生成缩略图的产品
发布时间:2020-12-13 17:47:11 所属栏目:PHP教程 来源:网络整理
导读:我正在为客户端创建自定义CSV导入程序并添加图片,但是缩略图未正确生成.使用像Regenerate Thumbnails这样的插件后,它们会正确显示. 这是我添加附件并将其链接到帖子的代码. $uploadDir = 'wp-content/uploads/importedproductimages/'; $siteurl = get_optio
我正在为客户端创建自定义CSV导入程序并添加图片,但是缩略图未正确生成.使用像Regenerate Thumbnails这样的插件后,它们会正确显示.
这是我添加附件并将其链接到帖子的代码. $uploadDir = 'wp-content/uploads/importedproductimages/'; $siteurl = get_option('siteurl'); $thumbnail = 'importedproductimages/' . $name; $filename = 'importedproductimages/' . $name; $wp_filetype = wp_check_filetype($filename,null); $attachment = array( 'post_author' => 1,'post_date' => current_time('mysql'),'post_date_gmt' => current_time('mysql'),'post_mime_type' => $wp_filetype['type'],'post_title' => $filename,'comment_status' => 'closed','ping_status' => 'closed','post_content' => '','post_status' => 'inherit','post_modified' => current_time('mysql'),'post_modified_gmt' => current_time('mysql'),'post_parent' => $post_id,'post_type' => 'attachment','guid' => $siteurl.'/'.$uploadDir.$name ); $attach_id = wp_insert_attachment( $attachment,$filename,$post_id ); $attach_data = wp_generate_attachment_metadata( $attach_id,$thumbnail ); wp_update_attachment_metadata( $attach_id,$attach_data ); // add featured image to post add_post_meta($post_id,'_thumbnail_id',$attach_id); 为什么缩略图没有正确生成? 编辑: 我也像image.php一样包括: require_once(ABSPATH . 'wp-admin/includes/image.php'); 解决方法
这最终为我工作:
function createnewproduct($product) { $new_post = array( 'post_title' => $product['Product'],'post_content' => $product['Long_description'],'post_status' => 'publish','post_type' => 'product' ); $skuu = $product['SKU']; $post_id = wp_insert_post($new_post); update_post_meta($post_id,'_sku',$skuu ); update_post_meta( $post_id,'_regular_price',$product['ourPrice'] ); update_post_meta( $post_id,'_manage_stock',true ); update_post_meta( $post_id,'_stock',$product['Qty'] ); update_post_meta( $post_id,'_weight',$product['Weight'] ); if (((int)$product['Qty']) > 0) { update_post_meta( $post_id,'_stock_status','instock'); } $dir = dirname(__FILE__); $imageFolder = $dir.'/../import/'; $imageFile = $product['ID'].'.jpg'; $imageFull = $imageFolder.$imageFile; // only need these if performing outside of admin environment require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); // example image $image = 'http://localhost/wordpress/wp-content/import/'.$product['ID'].'.jpg'; // magic sideload image returns an HTML image,not an ID $media = media_sideload_image($image,$post_id); // therefore we must find it so we can set it as featured ID if(!empty($media) && !is_wp_error($media)){ $args = array( 'post_type' => 'attachment','posts_per_page' => -1,'post_status' => 'any','post_parent' => $post_id ); // reference new image to set as featured $attachments = get_posts($args); if(isset($attachments) && is_array($attachments)){ foreach($attachments as $attachment){ // grab source of full size images (so no 300x150 nonsense in path) $image = wp_get_attachment_image_src($attachment->ID,'full'); // determine if in the $media image we created,the string of the URL exists if(strpos($media,$image[0]) !== false){ // if so,we found our image. set it as thumbnail set_post_thumbnail($post_id,$attachment->ID); // only want one image break; } } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |