I have a sidebar block that shows a list of links of topics related to the article that the user is currently reading. Currently I insert these links manually, but I'd like to generate them automatically using taxonomy terms and php. I know how to generate an array of the taxonomy vocabularies and terms associated with my nodes, but I don't know how to specify the term I want to use or how to insert that term into the urls I use in my block.

Below you can see I'm using an array I've defined in an external include file (baseball.php). I think all I really need to be able to do is use a variable in my urls that will be replaced with the lowercase word "baseball" (one of my taxonomy terms).

Ok, here's the block contents code:

<?php
include('./includes/myincludes/baseball.php');
?>

<a href="/articles/baseball/topic1"><?php echo $baseball["topic1"]; ?></a><br />
<a href="/lit/baseball/topic2"><?php echo $baseball["topic2"]; ?></a><br />
<a href="/lit/baseball/topic3"><?php echo $baseball["topic3"]; ?></a>

Here's the code I'm using to get the taxomony terms:

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
  $node = node_load(arg(1));
  $categories = taxonomy_node_get_terms($node->nid);
  print_r(array_values($categories));
}
?>

And here are the results I get when I call up the taxonomy terms for the current node:

Array ( 
[0] => stdClass Object ( [tid] => 3 [vid] => 2 [name] => Article Title [weight] => 0 ) 
[1] => stdClass Object ( [tid] => 20 [vid] => 2 [name] => Baseball [weight] => 0 ) 
[2] => stdClass Object ( [tid] => 5 [vid] => 2 [name] => Baseball Facts [weight] => 0 ) 

Any advice on the best way to do this would be MUCH appreciated. Thanks.

Comments

juroon’s picture

I need the answer to this question too. It's easy to generate a menu with taxonomy terms, but I need to modify the resulting URL, while including the taxonomy term.

juroon’s picture

I found something that works for me. The php I used in my block is posted here, where I found the solution: http://drupal.org/node/289831#comment-962094