【问题标题】:set featured image for WordPress post via XML-RPC通过 XML-RPC 为 WordPress 帖子设置特色图片
【发布时间】:2012-09-03 13:40:10
【问题描述】:

大约 6 个月前,WordPress API 进行了一次更新,允许设置帖子的缩略图(或特色)图像。

http://www.maxcutler.com/2012/04/04/xml-rpc-in-wordpress-3-4/

我正在尝试使用它,但它不适合我。我想知道我可能做错了什么。我正在调用 XML-RPC newPost 方法来创建帖子并传递媒体库中现有资产的媒体 ID(称为媒体库中的附件 ID)正在创建新帖子,并且正在创建所有其他属性集,除了特色图像。

我验证了我的 wordpress api 版本,果然在 class-wp-xmlrpc-server.php 中我看到了新帖子功能部分的评论: "* post_thumbnail - 用作帖子缩略图/特色图片的媒体项目的 ID"

所有其他属性都在工作。我可以通过 XML-RPC 将新图像添加到媒体库。我可以创建和更新帖子并设置它们的标签、标题、描述、自定义字段值和类别。当我尝试设置 post_thumbnail 值时,我没有收到任何错误。即使我传入了一个不存在的媒体 ID,这似乎很奇怪。

【问题讨论】:

    标签: vb.net wordpress xml-rpc


    【解决方案1】:

    啊!这张 WP 3.4 版票具有误导性! http://core.trac.wordpress.org/ticket/20396

    这是“wp_post_thumbnail”而不是“post_thumbnail”

    【讨论】:

      【解决方案2】:

      我试图用我的 Ruby 脚本和 XML RPC API 做同样的事情。

      • 首先初始化并连接到您的 wordpress 网站:

        wp = Rubypress::Client.new( :host => "your host",
            :username => "test",
            :password => "test",
            :path => "yourhost/xmlrpc.php"
          )
        
      • 上传您想要作为特色图片的图片。

        wp.uploadFile( :data => { :name => File.basename(FILENAME),
                      :type => "image/png",
                      :bits => XMLRPC::Base64.new(File.open(FILENAME).read)
                    }
                  )
        
      • 使用getMediaItem方法获取附件ID。

        attach = wp.getMediaItem(:blog_id => 0, :attachment_id => img_id.to_i)
        
        • 现在使用newPost 方法创建一个帖子

          wp.newPost( :blog_id => 0, # 0 unless using WP Multi-Site, then use the blog id
          :content => {
                       :post_status  => "draft",
                       :post_date    => Time.now,
                       :post_content => "This is the body",
                       :post_title   => "test title best!",
                       :post_name    => "test best",
                       :post_author  => 1,
                       :post_type=>'post',
                       :post_thumbnail => attach['attachment_id']
                       :terms_names  => {
                          :category   => ['Category One','Category'],
                          :post_tag => ['Tag One','Tag Two', 'Tag Three']
                                        },
          
                       }
          
          
          )
          
      • 通过getPost 方法检查结果,该方法将返回您的帖子

         get_data = wp.getPost(:post_id => new_post_resp.to_i, :blog_id => 0)
        

      您应该参考以下链接。这些都是我遇到同样问题时的发现:

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 2013-10-04
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        相关资源
        最近更新 更多