Wordpress Post通过XMLRPC - 添加多个类别

时间:2012-02-10 02:16:35

标签: php wordpress xml-rpc

我试图通过XMLRPC将多个类别添加到Wordpress(3.3.1)帖子中。

这是我的代码(工作正常,请在下面阅读):

<?
error_reporting(E_ALL);
ini_set('display_errors', '1');


require_once("IXR_Library.php.inc"); // http://www.hurricanesoftwares.com/php_uploads/IXR_Library.txt

$client->debug = true; //Set it to false in Production Environment

$title="Blog Title5"; // $title variable will insert your blog title 
$body = "teste xmlrpc <a href='http://www.teste.com'>teste.com</a>";

$category="DVDSCR, Telesync"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";

$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format


    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category),
        'custom_fields' =>  array($customfields)


    );

// Create the client object
$client = new IXR_Client('http://127.0.0.1/xmlrpc.php');

 $username = "admin"; 
 $password = "password"; 
 $params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'

// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
    die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
    echo "Article Posted Successfully";

?>

错误:
如果我尝试添加多个类别,则将帖子类别设置为未分类(默认)。

我已经尝试过这个:

$category = "telesync, dvdscr";

并且:

$category =array('telesync','dvdscr');

如何在帖子中添加多个类别? 谢谢大家!

2 个答案:

答案 0 :(得分:2)

我在测试了一些其他选项后找到了答案:

'categories'=>array("telesync", "1080p"),

$ content 变量如下所示:

$content = array(
    'title'=>$title,
    'description'=>$body,
    'mt_allow_comments'=>0,  // 1 to allow comments
    'mt_allow_pings'=>0,  // 1 to allow trackbacks
    'post_type'=>'post',
    'mt_keywords'=>$keywords,
    'categories'=>array("telesync", "1080p"), // I've typed the categories directly here.
    'custom_fields' =>  array($customfields)


);

答案 1 :(得分:0)

我知道这有点晚了但是对于那些遇到同样问题的人来说,第一个猜测是最好的解决方案(而不是直接输入类别,最好将它们作为变量传递):

$category =array('telesync','dvdscr');

我们只需删除categories=>array($category)上的'数组',因为我们已将$category声明为数组。所以而不是:

'categories'=>array($category),

使用:

'categories'=>$category,

它应该有效。