Getting node id inside a block template using Drupal 7

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

Thanks in advance.

+6
source share
2 answers

Assuming you are on the node page itself, the menu_get_object() function will return a node object:

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

Source: https://habr.com/ru/post/907319/


All Articles