I want to have a block to show only if the current page has a certain taxonomy term and was planning on using the block visiablity settings.

where Pages on which this PHP code returns TRUE (experts only). and need php code to find out how to tell check if taxonomy term is on the page and then the block will show up on nodes with that term.

2 Answers 2


The code posted by Triskelion did not work for me. Here is the code that I used and worked:

$term_id_to_trigger_show_block = 14; // show this block on nodes with this term id.
$taxonomy_to_search = "field_machinenameofvocab"; // The 'field_' + machine name of the vocabulary to search for the terms
$make_block_visible = FALSE;

// Show this block on nodes with the right term id 
if (arg(0) == 'node' && arg(1) && is_numeric(arg(1))) {
  $node_obj = node_load(arg(1));
  $taxonomy = $node_obj->$taxonomy_to_search;
  foreach ($taxonomy['und'] as $term) {
    if ($term['tid'] == $term_id_to_trigger_show_block) {
      $make_block_visible = TRUE;

return $make_block_visible;

Note that $taxonomy_to_search should be set equal to the machine name of the vocabulary prefixed with field_. So if my vocabulary machine name is animal_species that line would be $taxonomy_to_search = "field_animal_species";

UPDATE: After using the above code on a site for quite some time I realized that it was giving me PHP errors in my logs. My solution was to give up on displaying a standard block and use views instead.

Create a new block view, set it to display fields. For your only field use "Global: Custom Text," entering your desired block content in the custom text field.

Under "Contextual Filters" add "Content: NID" and then "Provide default value" of "Content ID from URL."

Finally under "Filter Criteria" your only filter should be "Content: Has taxonomy term." Set it up to look for your term.


To do this, the code you need would be like the following:

// ID of desired term
$term_tid = 3;
// Check if you are on a node page, and get the nid
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $nid = (int)arg(1);
  // Load the node object
  $node = node_load($nid);
  // Check for the existence and then check the value of the term ID
  if (isset($node->field_term[$node->language][0]['tid']) && $node->field_term[$node->language][0]['tid'] == $term_tid) {
    return TRUE;
  } else {
    return FALSE; }

Just substitute the name of your term reference field for field_term, and set the tid of the term you wish to use. If your term reference field allows multiple values, you will have to get a little more rigorous and look for the value in the array.

  • Thanks for the input and works and how can I be able to use and sub taxonomy terms or add more then term_id?
    – seth
    Commented May 29, 2013 at 20:12
  • This shows the logic used to get a value from a node, test it, and control the display of the block. To go beyond this I will need to know more about the real-life situation. How do you select the terms you want the block to display with? Does the term reference field have a single or multiple values? Expand your question.
    – Triskelion
    Commented May 29, 2013 at 20:23
  • say i add taxonomy child to my main term. For example say section is sports and and to child tax id of baseball and football and want it to show up those tax id also?
    – seth
    Commented May 29, 2013 at 20:33
  • No. Explain in detail what you are trying to do, not in terms of the code, but in terms of the site and how you want it to behave. The example I gave uses a hard-coded TID. How do you select which pages you want the block on, and which pages you do not want it on? Please edit your question and give more details.
    – Triskelion
    Commented May 29, 2013 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.