6
votes

Alright, so I've created a template file for one of my blocks, which works just fine. However inside this template I would like to get the id of the current node. How is that possible? I've tried arg, $node and all of these variables, but none of them are available.

Thanks in advance.

2
I love you for asking this question! ;) - Frederik Wordenskjold

2 Answers

23
votes

Assuming you're on the node page itself the menu_get_object() function will return the node object:

$node = menu_get_object();
if ($node && $node->nid) {
  // You have a valid node to work with.
}
0
votes
// it will gives only node id.
if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
}
echo $nid;