2011-01-11 72 views
0

我想用php打印某些節點,如果該節點有兩個特定的術語。Drupal 6:基於兩個分類術語的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'; 
     } 
    } 
?> 
+0

不知道Drupal代碼是否正確,但你的PHP語法不是。 `if`參數應該在parens中,你應該做一個比較檢查而不是賦值,所以你的`if`語句應該看起來像`if($ tid == 19)` – 2011-01-11 21:51:43

回答

0

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'; 
}