开发者

drupal---the taxonomy array

when i print "print_r($node)" in the node.tpl.php. i get this.

  taxonomy] => Array (
                                [1] => stdClass object (
                                                                     [tid] =>1
                                                                     [vid]=>1
                                                                     [name]=>cms
      ............)
      )

so from the above, I know the taxonomy is an array. the array's value is an object. That the question comes. I looked up the php manual again a开发者_StackOverflow社区nd again, didn't find there is a saying "the array's value can be a object" why,I can't follow the above's code well. Hope someone can explain it to me. Thank you.


What Pekka write:

echo $node["taxonomy"][1]->tid;

Is not wrong, in the sense that it works for the above example. However, since you are doing this in the node.tpl.php you probably want something more robust, than this, since it only works for nodes with the term that has id 1.

Since the array of taxonomy terms is of the format:

array(tid => term_object)

You need to know the tid to access the term object. If you however want the tid, you can just get the array key:

$tids = array_keys($node["taxonomy"]);

Now, you don't know if or how many terms there is associated to your node, as it can be changed via settings, if you did:

if (!empty($node["taxonomy"])) {
   $tids = array_keys($node["taxonomy"]);
   $tid = tids[0];
}

You would get the tid of the first term (the one with the lowest tid). If you know from your setup that the node can only have 1 term and since the theme you're doing this in is site specific, this will be good enough for you. Else $tids will be an array of all the tids for the node that you use for your wishes.


in other words, in php, the array's value can be anything. am i right.

Yes, an array can hold values of any data type.

how the code i should write

The "path" to access the variable you show above would be:

echo $node["taxonomy"][1]->tid;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜