A short question to which I cannot seem to find the answer:

I defined a block within my module. This block is displayed in every page but its contents depend on the node currently being diplayed (in page view).

How can I (from within my module) check which node is currently being displayed in the page?

Thanks a lot, Emdo

Comments

nevets’s picture

If you are talking about the node displayed when viewing pages like node/{nid} you can get the node like this

if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
}

[Edit to fix function name]

DWB Internet’s picture

Thanks Nevets, that's exactly what I was looking for.

beatnikdude’s picture

correction:
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
not:
if ( arg(0) == 'node' && s_numeric(arg(1)) ) {

torotil’s picture

Hi,

I answer to this old thread because it is rather well-positioned in google search results.

Since D6 there is a much better solution for this problem. You can get the current $node object without loading it from the database with:

$node =& menu_get_object();
pog21’s picture

Using menu_get_object() in a block did not work for me. For example, the following code prints nothing:

$node = menu_get_object();
print_r($node);

Is there something else that has to be done to make it available from within a block?

nevets’s picture

$node will only be set if viewing a single node, ie unaliased path of node/{nid}