user328146
user328146

Reputation:

Getting node id inside block template with Drupal 7

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.

Upvotes: 6

Views: 12485

Answers (2)

Dharma Raju
Dharma Raju

Reputation: 51

// it will gives only node id.
if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
}
echo $nid;

Upvotes: 0

Clive
Clive

Reputation: 36955

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.
}

Upvotes: 23

Related Questions