【问题标题】:How to set thumbnail to imported WordPress posts in bulk如何为批量导入的 WordPress 帖子设置缩略图
【发布时间】:2019-06-11 03:00:14
【问题描述】:

我从网站上抓取数据并将其存储到 XML 文件中(使用 python)。从其他网站下载图像并将它们存储到我的 WordPress 网站时,WordPress 为它们分配了一个我不知道的唯一 ID。所以我不能将该图像作为缩略图分配给我抓取的帖子。我无法一一编辑帖子,因为它们是批量的。有没有其他解决办法?

我可以在帖子上显示图像,但我需要知道 ID 才能将它们分配为缩略图。我还使用了自动将第一张图像分配为缩略图的插件,但它们也需要图像 ID...

【问题讨论】:

    标签: php python wordpress scrapy


    【解决方案1】:

    this question 可能重复。为此,您需要使用各种内置的 WordPress 功能:

    wp_insert_attachment()
    set_post_thumbnail()
    wp_generate_attachment_metadata()
    wp_update_attachment_metadata()
    

    wp_insert_attachment() 可能是您最重要的函数,因为它会返回您的附件(图像)ID。 set_post_thumbnail() 然后将这个附件 ID 和您的帖子 ID 设置为帖子中的特色图片。

    当我过去这样做时,我还发现我必须使用上面的元数据函数来正确分配用于从导入中删除帖子的图像它们的关联图像。

            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 );
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 2011-09-04
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多