2012-09-12 208 views
0

为IM V类试图插入类别插入标签通过PHP

$apples = "granny smith" 

为一个新的职位的一部分。

我可以用下面的脚本很容易地创建一个新的职位与TAG:

$my_post = array(
    'post_title' => $title, 
    'post_content' => $content, 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'tags_input' => $apples, 
    'post_type' => 'post' 
    ); 
wp_insert_post($my_post); 

但出于某种原因,尽管该WP抄本列出

'post_category' 

的类别,下面的代码不创建新的类别“奶奶史密斯”,而是作为“未分类”进入新的职位:

$my_post = array(
    'post_title' => $title, 
    'post_content' => $content, 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_category' => $apples 
    'post_type' => 'post' 
    ); 
wp_insert_post($my_post); 

有人可以帮助我的代码吗?我哪里错了?

回答

0

Accordning到the documentation它看起来像类别应增加与类别ID的数组:

$id_for_category_ganny_smith = 12; 
$my_post = array(
    'post_title' => $title, 
    'post_content' => $content, 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_category' => array($id_for_category_ganny_smith), 
    'post_type' => 'post' 
    ); 
wp_insert_post($my_post); 

的文档状态:

分类需要作为一个整数数组传递与数据库中的 类别ID匹配。即使只有一个 类别被分配到该帖子,情况也是如此。

如果你不知道你要添加的类别的ID,你可以尝试以下方法:

$apples_slug = "granny_smith" 
$my_post = array(
    'post_title' => $title, 
    'post_content' => $content, 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_type' => 'post' 
    ); 
$post_ID = wp_insert_post($my_post); 
wp_set_post_terms($post_ID, $apples_slug, 'category')