8

How can I check if the current page is a taxonomy term page, similarly to what done with the following code used to check if the current page is the front page?

if (drupal_is_front_page()) {
  // The current page is the front page.
}
else {
  // It is not the front page.
} 

3 Answers 3

13

Just to offer a solution that doesn't make use of arg() (which it's advised to avoid where possible according to the docs page)

You can use the menu_get_object() function:

$term = menu_get_object('taxonomy_term', 2);
if ($term) {
  // User is on a taxonomy term page
}
14
  • 1
    I guess above code also get executed in taxonomy/term/id/edit & feed pages too !! We may need to check for third argument ?
    – Anil Sagar
    Commented Oct 31, 2012 at 15:10
  • 1
    @AnilSagar Holy moly you're right :P I'm really not sure what the accepted way to do this is then, assuming I haven't made up that bit about arg() being less readable
    – Clive
    Commented Oct 31, 2012 at 15:14
  • How about popping a little && !path_is_admin(current_path()) in the if statement? I've not checked it yet, but term edit paths are admin pages, no :)
    – Chapabu
    Commented Oct 31, 2012 at 16:33
  • 1
    @Chapabu You should trademark that ;)
    – Clive
    Commented Oct 31, 2012 at 16:41
  • 2
    Caution: Enabling Views replaces the taxonomy term loader, causing menu_get_object('taxonomy_term', 2); to not work anymore. See: drupal.org/node/1468006 and api.drupal.org/comment/45018#comment-45018
    – Manu
    Commented Sep 6, 2013 at 9:08
5
<?php

if (arg(0) ==  "taxonomy" && arg(1) == "term" && is_numeric(arg(2)) && arg(3) == "") {
  // Taxonomy term page
}

?>
5

<?php if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2)): ?> should do the trick.

In case edit/feed (and possibly some other related pages) should be excluded you can use: <?php if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2) && !arg(3)): ?>

2
  • That will also return true for taxonomy/term/123/edit which I'm not sure the OP wants.
    – Clive
    Commented Oct 31, 2012 at 15:07
  • 1
    Yeah he didn't say what he is aiming for precisely, but I've updated my answer to address that concern.
    – Madis
    Commented Oct 31, 2012 at 16:30

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.