【问题标题】:Using Python & XML-RPC to add a custom field to a Wordpress post?使用 Python 和 XML-RPC 向 Wordpress 帖子添加自定义字段?
【发布时间】:2010-03-10 19:11:16
【问题描述】:

我想使用 Python 和 XML-RPC 将“附件”自定义字段添加到现有的 Wordpress 帖子中。

我的代码如下所示:

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.'>

我做错了什么?

【问题讨论】:

    标签: python wordpress xml-rpc


    【解决方案1】:

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

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

    尝试使用 metaWeblog.getPost 获取已包含自定义字段的帖子的帖子数据,您会看到它们的样子。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多