Monthly Archives: May 2011

Displaying a block based on the taxonomy term in the URL

I wanted to display node content in a block based on it’s taxonomy term’s URL, so that from the user can see a list of nodes of term exterior. The problem is that blocks don’t pull arguments from the URL same way as a page view. This can be done by adding a term:ID argument, providing a default argument as php code. The following snippet takes the term id so that Drupal can use it as an argument for the content to be displayed:

//break path into an array
$path = explode(‘/’, $path);
$termname = array_pop($path);
//convert the dashes to spaces
$termname = str_replace(“-“, ” “, $termname);
$terms = taxonomy_get_term_by_name($termname );
$term = reset($terms);
return $term->tid;

More on this: