我可以直接从数据库检索词汇表ID, 但是有没有内置的函数可以做到这一点?
例如:
我有一个词汇叫做“listing”, 我需要内置函数将“列表”作为函数参数,然后返回 一个视频。
我正在使用 drupal 6
最佳答案
我有一个函数,几乎......
/**
* This function will return a vocabulary object which matches the
* given name. Will return null if no such vocabulary exists.
*
* @param String $vocabulary_name
* This is the name of the section which is required
* @return Object
* This is the vocabulary object with the name
* or null if no such vocabulary exists
*/
function mymodule_get_vocabulary_by_name($vocabulary_name) {
$vocabs = taxonomy_get_vocabularies(NULL);
foreach ($vocabs as $vocab_object) {
if ($vocab_object->name == $vocabulary_name) {
return $vocab_object;
}
}
return NULL;
}
如果您想要 vid,只需获取返回对象的 vid 属性即可。
$vocab_object = mymodule_get_vocabulary_by_name("listing");
$my_vid = $vocab_object->vid;
Henrik关于将其存储在变量中的观点非常有效,因为您不希望在每个请求上运行上述代码。
编辑
还值得注意的是,在 Drupal 7 中您可以使用 taxonomy_vocabulary_get_names()这使得这变得更容易一些。
关于drupal - 按名称获取词汇 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639932/