Drupal 7 API +分类标准
问题描述:
我试图用PHP构建一个小工具,从我当前的CMS导入内容到Drupal 7,因为我有大约10k +的文章引入到目前为止,我已经得到了标题,摘要,正文,作者和发布日期来通过,但是当涉及类别(标签)时,我完全困惑。Drupal 7 API +分类标准
我当前的每个类别/标签都存储在数据库表中,每个表都有自己的ID,名称和说明。我可以把这个出来,每个文章和排序,但我想(字符串,数组等)。
在我进口,我猜我应该做这样的事情:
$node->field_tags = array(
'und' => array(
array(
'Update',
'News',
'Report'
)
)
);
我也试过:
$node->field_tags = array(
'Update',
'News',
'Report'
);
但这些也不是逗号分隔的单词串料不起作用。 Drupal 7 API文档似乎没有解释我发现的任何地方。
什么是通过发送标签的格式或什么是我无法找到的文档页面?先谢谢你!
答
Drupal 7中的术语字段与物理分类术语相关,因此您需要为每个类别创建一个术语,然后将该引用作为字段的值添加。
此代码可以帮助:
// Load the tags vocabulary
$vocab = taxonomy_vocabulary_machine_name_load('tags');
$term = new stdClass;
$term->vid = $vocab->vid; // Attach the vocab id to the new term
$term->name = 'Category Name'; // Give the term a name
taxonomy_term_save($term); // Save it
// Add the tags field
$node->field_tags = array(
LANGUAGE_NONE => array(
'tid' => $term->tid // Relate the field to the new category term using it's tid (term id)
)
);
谢谢您的回复!如果我想添加多个标签怎么办?他们当然不能都拥有'tid'指数。 – devincrisis
是的,您添加的每个标签都是分类术语,每个分类术语都需要独立存在,否则Drupal没有任何关联。 – Clive