【问题标题】:Array nested in hash for WP API?数组嵌套在 WP API 的哈希中?
【发布时间】:2012-08-02 09:00:29
【问题描述】:

这是我的电话:

 result = blog.call('wp.newPost',
                   1,
                   'user',
                   'pw',
                   { 
                     'post_type' => 'post', 
                     'post_content' => entry[3], 
                     'post_name' => entry[2].downcase.split(" ").join("-"),
                     'comment_status' => 'closed',
                     'pinged' => 'closed',
                     'post_status' => 'publish',
                     'post_title' => entry[2],
                     'terms' => ['category' => 9]
                   })

这将返回一个错误,即此帖子类型 post 不支持给定 category 的分类法之一 - 好吧,每个帖子都应该有一个类别,所以我认为我的 ruby​​ 格式不正确。 API 要求一个数组,其中分类法作为键,其 ID 作为值,我想我已经在这里完成了。

这适用于 v3.4 - here is the documentation on wp.newPost

【问题讨论】:

  • 很难在没有看到blog.call 方法的情况下发表评论。但是,API 文档要求一个以分类名称为键的数组和一个以术语 ID 为值的数组。所以,如果9 是您要分配给帖子的术语的ID,我猜它应该是'terms' => ['category' => [9]]
  • 另外,我对 Ruby 不熟悉,但我认为您不能使用该语言指定数组键,因此您可能需要使用哈希:'terms' => {'category' => [9]}
  • @RichardM 实际上,这两个都不起作用-我确实认为这与数组格式有关。第一个产生了相同的错误,第二个产生了 302 found 错误(它基本上完全忽略了该行)

标签: ruby wordpress xml-rpc


【解决方案1】:

有趣的是,以下代码有效:

    blogcontent = {
        :post_type => 'post',
        :post_content => entry[3],
        :post_name => entry[2].downcase.split(" ").join("-"), 
        :comment_status => 'closed', 
        :pinged => 'closed',
        :post_status => 'publish',
        :post_title => entry[2],
        :terms =>
            {
                :category => [9]
            }
  }

这将(通过 XMLRPC 编写器)转换为适当的 XML 并在 WordPress 中注册帖子。打开 XML-RPC 调试信息显示,除非变量 9 括在括号中,否则不会传递结构,即使它是单值数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2018-09-15
    • 2019-11-21
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多