通过XML-RPC为WordPress帖子设置特色图片



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

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

我试着用它,但它对我不起作用。我想知道我可能做错了什么。我调用XML-RPC newPost方法来创建帖子,并传递媒体库中现有资产的媒体ID(在媒体库中称为attachment_ID)。正在创建新帖子,并设置除特色图像外的所有其他属性。

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

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

arrrg!此WP 3.4版票证具有误导性!http://core.trac.wordpress.org/ticket/20396

它是"wp_post_thumbnail"而不是"post_thumbnil"

我试图用Ruby脚本和XMLRPCneneneba 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)
    

您应该参考以下链接。这些都是我在面临相同问题时的发现:

  • 参考文献-1
  • 参考文献-2
  • 参考文献-3
  • 参考文献-4
  • 参考文献-5

最新更新