There is a way to get $node->nid, inside a block ?
I wanna know whats the current node Id, but global $node doesnt seems to have this variable in blocks.

Comments

masande’s picture

the arg() function (http://api.drupal.org/api/function/arg/5)pulls components from a drupal url. perhaps this will return the current node->id?

if the url that being accessed is http://www.example.com/node/4, try the following php in your block:

$nid = arg(1);
print $nid;

Mark Sanders
Q Collective

Mark Sanders
Q Collective

Laurenzio’s picture

I think this is working only for somethink like: node/55

the problem is that arg(1) is not always my node id, since the url is not always the same.

masande’s picture

if the position of the nid changes in the url, here is how to find the $index value for the arg() function:

/this/is/the/nid >> 'this' = arg(0), 'is' = arg(1), 'the' = arg(2), 'nid' = arg(3), and so on...

also, you may want to add the following logic to make sure the url has a numeric nid:

<?php
if (is_numeric(arg(1))) {
  $nid = arg(1);
  print $nid;
}
?>

if this doesn't work, could you provide an example of the paths you are using to access your nodes? that way i can be a lot more specific.

Mark Sanders
Q Collective

Mark Sanders
Q Collective

Laurenzio’s picture

great ! thanks

alienzed’s picture

this works even when using path aliases!

nevets’s picture

A more current approach would be

$node = menu_get_object();
if ( !empty($node) ) {
  print $node->nid;
}