Skip to main content
2 of 4
added 157 characters in body
Clive
  • 168k
  • 19
  • 303
  • 337

I think menu_get_object() might be helpful:

menu_get_object() provides access to objects loaded by the current router item. For example, on the page node/%node, the router loads the %node object, and calling menu_get_object() will return that. Normally, it is necessary to specify the type of object referenced, however node is the default. The following example tests to see whether the node being displayed is of the "story" content type:

<?php
$node = menu_get_object();
$story = $node->type == 'story';
?>

You can use this with any router-loaded object so it stands to reason that on entity pages (including taxonomy term pages) that this would get you the correct object:

$entity = menu_get_object($entity_type);

UPDATE

Deleted as this method is actually no better than OP's method, it requires that you pass the position of the ID in the URL to the function.

Clive
  • 168k
  • 19
  • 303
  • 337