立即以编程方式保存嵌套分类术语

时间:2010-12-18 13:53:21

标签: drupal taxonomy drupal-taxonomy

以下代码允许使用Drupal API创建分类术语

$terms = array(   

      $term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term2 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term3 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),
);

foreach ($terms as $term) {
$term = (object) $term;
taxonomy_term_save($term);

}

它适用于兄弟术语但是如果我需要创建nester分类树怎么办?有“父”键应该包含要执行该操作的父术语ID数组

在父条款保存到DB之前,我如何知道这些ID?

1 个答案:

答案 0 :(得分:4)

通过taxonomy_term_save(更具体地说,taxonomy_term_save调用drupal_write_record)将密钥添加到传递的术语对象中:

$term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
);
$term1 = (object) $term1;
taxonomy_term_save($term1);
echo $term1->tid; // now where did that come from?