2

Possible Duplicate:
Check if current page is taxonomy term

For now, I use something like this to decide, if I'm on a taxonomy page.

if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
   $tid = intval(arg(2));
   $parents = taxonomy_get_parents($tid);
   $term = $parents[0];

   ...
}

Is there a more elegant way to decide, if I'm on a taxonomy page than this arg(0), arg(1), arg(2) thing? Same would apply to a node page if (arg(0) == 'node' && is_numeric(arg(1))).

I didn't find any reference in the net. But maybe I ask the wrong questions.

4

You could try menu_get_object(), it's slightly cleaner:

$term = menu_get_object('taxonomy_term', 2);
if ($term) {
  // You're on a term page and have the loaded object in $term
}
  • menu_get_object() is the way to go, as it always returns the correct object, especially for nodes, and URLs like node/1/revision/3. I would only add to be sure the function is already getting the required object without calling menu_get_object(), such as in the case of preprocess functions. – kiamlaluno Dec 13 '12 at 12:09
  • Thank's this is a good starting point. I wouldn't have thought about a function with 'menu' in it to be the solution. But unfortunately it's not exactely the right answer. Isn't there a function that answers the question: "What kind of page is this?". The menu_get_object just answers the question "Is this a node?" or "Is this a taxonomy_term?". – yunzen Dec 19 '12 at 9:44
  • Sorry if I have the misunderstood the question, but this does provide the node type too, or "What kind of page is this?". It "provides access to objects loaded by the current router item", thus the whole node/taxonomy object. In the case of a node, taken from the doc comments, the following example tests to see whether the node being displayed is of the "story" content type: ``` $node = menu_get_object(); $story = $node->type == 'story'; ``` – mediaashley Jan 9 '14 at 9:39

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