#连接到您的 WordPress 博客:
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods import media, posts
wp = Client('http://example.com/xmlrpc.php', 'username', 'password')
#获取媒体库中的图片列表:
attachments = wp.call(media.GetMediaLibrary({"number":500}))
#对图片列表进行分组,按名称前缀相同的图片分为一组:
grouped_attachments = {}
for attachment in attachments:
prefix = attachment['title'].split('_')[0]
if prefix not in grouped_attachments:
grouped_attachments[prefix] = []
grouped_attachments[prefix].append(attachment)
#循环遍历图片分组,并将名称前缀相同的图片作为一篇文章发布:
for prefix, attachments in grouped_attachments.items():
post = WordPressPost()
post.title = prefix
post_content = ''
for attachment in attachments:
post_content += '\n\n<img src="%s">' % (attachment['link'])
post.content = post_content
post.post_status = 'publish'
wp.call(posts.NewPost(post))
这样,所有名称前缀相同的图片将作为一篇文章发布,并使用图片的名称前缀作为文章标题。