【问题标题】:Wordpress Post Via XMLRPC - Add Multiple CategoriesWordpress 通过 XMLRPC 发布 - 添加多个类别
【发布时间】:2012-03-02 13:44:49
【问题描述】:

我正在尝试通过 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');

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

【问题讨论】:

    标签: php wordpress xml-rpc


    【解决方案1】:

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

    '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)
    
    
    );
    

    【讨论】:

      【解决方案2】:

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

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

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

      'categories'=>array($category),
      

      使用:

      'categories'=>$category,
      

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-14
        • 2011-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        • 2023-04-03
        • 1970-01-01
        相关资源
        最近更新 更多