【问题标题】:Drupal 6: PHP print based on two taxonomy termsDrupal 6:基于两个分类术语的 PHP 打印
【发布时间】:2011-01-11 21:29:07
【问题描述】:

如果节点有两个特定术语,我正在尝试使用 php 打印一些内容。

类似:

<?php
    $terms = taxonomy_node_get_terms($node, $key = 'tid');
    foreach ($terms as $term) {
        if ($term->tid == 19) {
            print '19';
        }
        if ($term->tid == 21) {
            print '21';
        }
    }
?>

【问题讨论】:

  • 不确定 Drupal 代码是否正确,但您的 PHP 语法不正确。 if 参数应该在括号中,并且您应该进行比较检查而不是赋值,因此您的两个 if 语句应该看起来像 if($tid == 19)
  • 我想通了并相应地更改了代码。发现我需要添加一个 foreach() 命令。

标签: php drupal drupal-6 taxonomy


【解决方案1】:

taxonomy_node_get_terms() 返回一个以术语 ID 作为数组键的数组。因此,要执行您想要的逻辑,您需要以下内容:

$tids = taxonomy_node_get_terms($node); 
// a $node object is passed as the only parameter in this case, so you would need to use node_load to get that
// the function has an optional second parameter, and 'tid' is the default, so it's not needed
$tids = array_keys($tids);
if (in_array(19, $tids)) {
  print '19';
}
if (in_array(21, $tids)) {
  print '21';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多