使用wp_set_object_terms添加自定义分类术语

时间:2018-06-25 09:48:19

标签: php wordpress

我正尝试使用以下命令将两个字符串添加为自定义分类术语:

$breadcrumbgrandparent = strip_tags($html->find('div[class=breadcrumb-panel] ul li', 1));
$breadcrumbparent = strip_tags($html->find('div[class=breadcrumb-panel] ul li', 2));
$tags = $breadcrumbgrandparent . ", " . $breadcrumbparent;

// vars
$my_post = array(
  'post_title'  => $strippedtitle, //site
  'post_type'       => 'product',
  'post_status' => 'publish',
);

// insert the post into the database
$post_id = wp_insert_post( $my_post );

$taxonomy = 'categories';
wp_set_object_terms($post_id, $tags, $taxonomy);

目前,它将字符串添加为一个术语,称为“ Term 1,Term 2”,而不是两个单独的术语,称为“ Term 1”和“ Term 2”。

我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

问题出在$ tags值上。它应该是一个数组,但是您使用的是内嵌字符串。

尝试一下:

wp_set_object_terms($post_id, explode(",",$tags), $taxonomy);