为了使您的帖子具有默认图像,您需要设置帖子缩略图。在执行此操作时,您需要设置媒体的ID,但这并不明显。
我的大部分工作都是用Python完成的,因此对我来说,以下内容很有帮助:
Step 1. 获取所有媒体的列表,以便了解ID
##
## Retrieve a list of media
# curl -X OPTIONS -i http://demo.wp-api.org/wp-json/wp/v2/posts
import json
import pycurl
import re
from io import BytesIO
import pandas as pd
import datetime
import urllib3
wpUrl = "https://MyWordPRessSite.COM</wp-json/wp/v2/media?page={}"
bContinue = True
page=1
while bContinue == True:
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.HTTPHEADER,[\'Content-Type: application/json\'])
myUrl = wpUrl.format(page)
#print(myUrl)
c.setopt(c.URL,myUrl)
c.perform()
page+= 1
if buffer != None:
myData = json.loads(buffer.getvalue())
for foo in myData:
print("MediaID ={}, caption = {}, alt_text={}".format(foo["id"], foo["caption"], foo[\'alt_text\']))
#print(foo)
if len(myData) <= 0:
bContinue = False
else:
bContinue = False
c.close()
Step 2. 使用正确的媒体ID创建帖子
######################################################
# Create A Post
######################################################
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
#authenticate
wp_url = "https://info-qa.cloudquant.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))