2011-03-17 81 views
0
function garland_separate_terms($node_taxonomy) { 
    if ($node_taxonomy) { 

foreach ($node_taxonomy AS $term) { 
$links[$term->vid]['taxonomy_term_'. $term->tid] = array(
    'title' => $term->name, 
    'href' => taxonomy_term_path($term), 
'attributes' => array(
    'rel' => 'tag', 
    'title' => strip_tags($term->description) 
    ), 
); 
} 
    //theming terms out 
    foreach ($links AS $key => $vid) { 
$terms[$key] = theme_links($vid); 
    } 
    } 
     return $terms; 
    } 

我無法很好地理解此功能。用於分類分類術語的功能

  1. 爲什麼作者沒有將$ node_taxonomy聲明爲數組($node_taxonomy=array())
  2. 這個$links[$term->vid]['taxonomy_term_'. $term->tid]來自哪裏?

回答

1

他期望$node_taxonomy包含特定節點的所有術語。 Each term is an object that contains attributes like vid,tid,name,description and path.

$ links是他創建的一個新數組。

因此,基本上,如果particualr節點具有條款A1,A2,從詞彙和術語B1 A3,B2從詞彙B,則陣列將其存儲爲

$links[a][a1] = details of a1 to convert into link 

$links[a][a2] = details of a2 to convert into link 

$links[a][a3] = details of a3 to convert into link 

$links[b][b1] = details of b1 to convert into link 

$links[b][b2] = details of b2 to convert into link 

最後,他被主題化的每個元素$鏈接使用theme_links()函數。

因此,最後你會得到所有術語的列表,按照詞彙表分組。

+0

謝謝!你應該怎麼知道術語是一個對象?$鏈接是他正在創建的一個新數組。是否有必要在使用它之前聲明$ links = array() – enjoylife 2011-03-18 01:52:49

+1

基本上,返回條件的drupal函數將它們作爲對象返回。 關於$ links = array(),這不是必須的,但它是一個很好的編程習慣。 – 2011-03-19 16:12:34