使用 Python &XML-RPC 将自定义字段添加到 Wordpress 帖子? [英] Using Python & XML-RPC to add a custom field to a Wordpress post?

查看:33
本文介绍了使用 Python &XML-RPC 将自定义字段添加到 Wordpress 帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python & 向现有的 Wordpress 帖子添加一个外壳"自定义字段.XML-RPC.

I want to add an 'enclosure' custom field to an existing Wordpress post using Python & XML-RPC.

我的代码如下所示:

def add_enclosure(server, post_id, enclosure):
    post_data = server.metaWeblog.getPost(post_id, username, password)
    custom_fields = post_data['custom_fields']
    new_id = max([int(field['id']) for field in custom_fields]) + 1

    custom_fields.append({'id': "%s" % (new_id), 'key': 'enclosure', \
                            'value': "%s\n%s\n%s" % \
                            (enclosure['url'], enclosure['length'], enclosure['type'])})
    server.metaWeblog.editPost(post_id, username, password, \
                                {'custom_fields': custom_fields})

但我收到以下错误:

xmlrpclib.Fault: <Fault 500: 'Sorry, your entry could not be edited. Something wrong happened.'>

我做错了什么?

推荐答案

自定义字段是一组键/值对,如下所示:

Custom fields are a set of key/value pairs that look like:

"custom_fields" = (
        {key = city; value = Sacramento; },
        {key = city; value = Sandy; }
    )

尝试使用 metaWeblog.getPost 为已经具有自定义字段的帖子获取帖子数据,您将看到它们的样子.

Try fetching post data with metaWeblog.getPost for a post that already has custom fields and you'll see what they look like.

这篇关于使用 Python &amp;XML-RPC 将自定义字段添加到 Wordpress 帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆