简体   繁体   English

使用 Python-xmlrpc 从外部图像链接设置 Wordpress 帖子缩略图

[英]Set Wordpress Post Thumbnail from External image link using Python-xmlrpc

After a lot of Google I landed up here: How can I set a post Thumbnail using an external image link from an instead of attachment id?经过大量的谷歌,我登陆这里:如何使用来自而不是附件 ID 的外部图像链接设置帖子缩略图? . .

Here is all that I can find, however I can't change it to set the thumbnail from an external image link.这是我能找到的所有内容,但是我无法更改它以从外部图像链接设置缩略图。

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost

#authenticate
wp_url = "https://blog.com/xmlrpc.php"
wp_username = "My_User_ID_on_WP"
wp_password = "My_PWD_on_WP"


wp = Client(wp_url, wp_username, wp_password)

#post and activate new post
post = WordPressPost()
post.title = '3 Post'
post.content = '<h1>heading 1</h1>Tayloe was here<br><small>here too!</small><p>New para.'
post.post_status = 'draft'
post.thumbnail = 50  # The ID of the image determined in Step 1
post.slug = "123abc"
post.terms_names = {
  'post_tag': ['MyTag'],
  'category': ['Category']
}
wp.call(NewPost(post))

Note : I don't want to save image on my servers and use an external image only注意:我不想在我的服务器上保存图像而只使用外部图像

You could try downloading the image, uploading it to your Wordpress site, and using the ID for the thumbnail parameter as you are above:您可以尝试下载图像,将其上传到您的 Wordpress 站点,并使用上面的缩略图参数的 ID:

import base64
import requests

# ...

# raw string url
url = r'https://png.pngtree.com/element_our/20190530/ourmid/pngtree-cartoon-google-icon-download-image_1257171.jpg'

# retrieve and store base64 encoded image and mime-type
r = requests.get(url)
mime_type = r.headers['Content-Type']
img_base64 = base64.b64encode(r.content)

upload_image_dict = {
    'name': 'some_file_name.ext',
    'type': mime_type,
    'bits': img_base64,
    'overwrite': True
}

resp = wordpress_xmlrpc.methods.media.UploadFile(upload_image_dict)

image_id = resp['id']

# ... [ your code here]

post.thumbnail = image_id

# ...

https://python-wordpress-xmlrpc.readthedocs.io/en/latest/ref/methods.html#wordpress_xmlrpc.methods.media.UploadFile https://python-wordpress-xmlrpc.readthedocs.io/en/latest/ref/methods.html#wordpress_xmlrpc.methods.media.UploadFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python创建一个新的wordpress帖子(xmlrpc或wordpress_xmlrpc) - Create a new wordpress post using Python (xmlrpc or wordpress_xmlrpc) 使用Python和Wordpress的XMLRPC的SSL问题 - SSL issue with XMLRPC using Python and Wordpress 使用python-wordpress-xmlrpc发布时无法设置页面模板 - Cannot set the page template when posting using python-wordpress-xmlrpc Python-python-wordpress-xmlrpc将标签添加到具有已知ID的帖子中 - Python - python-wordpress-xmlrpc add tags to a post with known id Python WordPress REST API - 将特色图片发布为外部 URL - Python WordPress REST API - Post Featured Image as external URL 与Wordpress的xmlrpc库python会面 - Meeting with wordpress' xmlrpc library python 用xmlrpc lib发布的Wordpress发布状态 - Wordpress post status published with xmlrpc lib 我无法通过 wordpress_xmlrpc 从 python 发布到我的 wordpress 网站我得到 ExpatError: not well-formed (invalid token): line 1, column 222 - i can't post from python to my wordpress site by wordpress_xmlrpc i get ExpatError: not well-formed (invalid token): line 1, column 222 使用谷歌应用引擎python将图像从外部链接上传到谷歌云存储 - Uploading an Image from an external link to google cloud storage using google app engine python Python Wordpress_XMLRPC NewComment身份验证 - Python Wordpress_XMLRPC NewComment Auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM