网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

如何批量设置导入的WordPress帖子的缩略图

GG网络技术分享 2025-03-18 16:12 4


问题描述:

I scrape data from a website and store it into an XML file (using python). While downloading images from other website and storing them into my WordPress site, the WordPress assign them a unique ID which I don\'t know. So I can\'t assign that image as a thumbnail to my scraped posts. I can\'t edit posts one by one because they are in bulk. Is there any other solution?

I can display images on posts but I need to know the ID to assign them as a thumbnail. I also used plugins which auto assign the first image as a thumbnail but they also require image ID...

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我从网站抓取数据并将其存储到XML文件中(使用python)。 从其他网站下载图像并将其存储到我的WordPress网站时,WordPress会为它们分配一个我不知道的唯一ID。 因此,我无法将该图像作为缩略图分配给我的已删除帖子。 我不能逐个编辑帖子,因为它们是批量的。 还有其他解决方案吗?</ p>

我可以在帖子上显示图像,但我需要知道ID以将它们指定为缩略图。 我还使用插件自动将第一个图像分配为缩略图,但它们也需要图像ID ... </ p>

</ div>

网友观点:

Possible duplicate of this question. You need to use a variety of built-in WordPress functions for this:

wp_insert_attachment()

set_post_thumbnail()

wp_generate_attachment_metadata()

wp_update_attachment_metadata()

wp_insert_attachment() is probably your most important function as it returns your attachment (image) ID. set_post_thumbnail() then takes this attachment ID and your post ID to set the image as the featured image in the post.

When I\'ve done this in the past I also found that I had to use the metadata functions above to correctly assign the image which from memory was for removing posts from an import and their associated images. For a complete example for this see here

        file_put_contents($oldfile, $image_data);

rename( $oldfile, $newfile );

$wp_filetype = wp_check_filetype($filename, null );

$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, $newfile, $post_id );

if ( $set_thumb ) {

$res2= set_post_thumbnail( $post_id, $attach_id );

}

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

$update_attach_metadata = wp_update_attachment_metadata( $attach_id, $attach_data );

标签:

提交需求或反馈

Demand feedback