从xml-data中获取多个类别,并在wp_insert_post post_category中使用它

时间:2011-04-03 13:03:38

标签: php arrays wordpress

我在foreach循环中使用以下while循环来从xml文件中获取类别:

foreach($mm_rss_xml->channel->item as $mm_item) {

$i = 0;
while ($xml_getinfo_result->movies->movie->categories->category[$i]) {
$tmdb_cats = $xml_getinfo_result->movies->movie->categories->category[$i]->attributes()->name;  // TMDb Categories
echo "<li>".tmdb_cats."</li>";
$i++;
}

给了我:

  • 动作
  • 惊悚

现在,我如何将这些类别添加到wordpress类别中? (它们已经在wordpress中添加,因此不再需要添加它们)

我使用以下内容将帖子添加到WP:

$my_post = array();
    $my_post['post_title'] = $tmdb_moviename;
    $my_post['post_content'] = $mm_overview;
    $my_post['post_category'] = // I am stuck here...
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['tags_input'] = $tmdb_actors2;
    $my_post['filter'] = true;

$posted_id = wp_insert_post($my_post);

add_post_meta($posted_id, 'test', $average_rating);

} // end foreach loop

提前感谢您的回答:)

1 个答案:

答案 0 :(得分:0)

我自己想通了:

$cats = array();
while ($xml_getinfo_result->movies->movie->categories->category[$i]) {
    $tmdb_cats = $xml_getinfo_result->movies->movie->categories->category[$i]->attributes()->name; // TMDb Categories
    array_push($cats, get_cat_ID($tmdb_cats));
echo "<li>".tmdb_cats."</li>";
    $i++;   
}

然后将数组放在这里:

$my_post = array(); // build the needed array for the post itself
    $my_post['post_title'] = $tmdb_moviename;
    $my_post['post_content'] = $mm_overview;
    $my_post['post_category'] = $cats;
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = 1;
    $my_post['tags_input'] = $tmdb_actors2;
    $my_post['filter'] = true;