I am trying to get some content to show up when some conditions are verified: if I am on a certain page, the content will be shown; differently, the content is blank.
Everything looks correct; I just don't know if I am not using the correct function or something else is wrong.

<?php if (!taxonomy_term_is_page('18')): ?>
  <h5><a href="home/news">blah 1 News</a></h5>
<?php elseif (!taxonomy_term_is_page('28')): ?>
  <h5><a href="home/news">blah 2 News</a></h5>
<?php elseif (!taxonomy_term_is_page('29')): ?>
  <h5><a href="home/news">blah 3 News</a></h5>
<?php elseif (!taxonomy_term_is_page('30')): ?>
  <h5><a href="home/news">blah 4 News</a></h5>
<?php elseif (!taxonomy_term_is_page('81')): ?>
  <h5><a href="home/news">blah 5 News</a></h5>
<?php endif; ?>

This displays the results; however, when I am on one of the other terms the output doesn't change: it stays the same as the output for the taxonomy term with ID equal to 18.

My description for what I was trying to achieve has been changed; I am not trying to get anything shown based on any conditions. To put it in better words, I am trying to have the content shown; if I am on taxonomy/term/18, the content would show the else nothing would shown if I am on taxonomy/term/28 content shows and so on. It's simular to is_front_page function and you have content shown if you are on the front page, except these are taxonomy terms.

  • Why is there a "!" in the condition? Right now, you are saying if this is not the page for taxonomy term 18, display blah 1 news. You probably want to remove that... – Berdir Jun 2 '11 at 22:28
  • when i remove it nothing displays what do you suggest i put instead? – Jeremy Love Jun 2 '11 at 22:32
  • Don't use taxonomy_term_is_page() in the first place :) See kiamlaluno's updated answer. – Berdir Jun 2 '11 at 22:44

Basing on the documentation of taxonomy_term_is_page(), the function just checks if the current page is the page of the passed-in term. Looking at the code, the function verifies the path of the current page is "taxonomy/term/%term-id", and "%term-id" is the term ID of the taxonomy term passed as argument.

function taxonomy_term_is_page($term) {
  $page_term = menu_get_object('taxonomy_term', 2);
  return (!empty($page_term) ? $page_term->tid == $term->tid : FALSE);

You are passing to the function the wrong parameters; you should pass it a taxonomy term object.
The code you reported is calling the function as taxonomy_term_is_page('28').

If you know the term ID for the taxonomy term, then you can use the following code:

  $page_term = menu_get_object('taxonomy_term', 2);
<?php if (!empty($page_term)): ?>
  <?php if ($page_term->tid != 18): ?>
    <h5><a href="home/news">blah 1 News</a></h5>
  <?php elseif ($page_term->tid != 28): ?>
    <h5><a href="home/news">blah 2 News</a></h5>
  <?php elseif ($page_term->tid != 29)): ?>
    <h5><a href="home/news">blah 3 News</a></h5>
  <?php elseif ($page_term->tid != 30): ?>
    <h5><a href="home/news">blah 4 News</a></h5>
  <?php elseif ($page_term->tid != 81): ?>
    <h5><a href="home/news">blah 5 News</a></h5>
  <?php endif; ?>
<?php endif; ?>
  • yes i saw that but i need the function to work from a view theme not from in the functions page thats why i did a if else statement. – Jeremy Love Jun 2 '11 at 21:07
  • @Jeremy Love See my updated answer. – kiamlaluno Jun 2 '11 at 21:16
  • its the same code? – Jeremy Love Jun 2 '11 at 21:18
  • @Jeremy Love taxonomy_term_is_page() is the function you are calling, but as you see the parameters you are passing are not correct; you should pass just a taxonomy term object to the function, not a string and an integer. – kiamlaluno Jun 2 '11 at 21:34
  • 1
    @Jeremy: Also, it is cleaner and the "Drupal way" to place logic like that in a preprocess function, save it in $variables['title'] for example, and in your template, just print $title. – Berdir Jun 2 '11 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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