How to get the current node id

vj0914 - June 19, 2008 - 14:55

I created a custom page, in the page I use php code as input format.

here is what I did:

<?php
  
global $node;

  
$node_id = $node->nid;
?>

I thought this would give the nid of current node, but it apparently not, can any expert tell me how to do it? much appreciated.

the nid will depend on

vj0914 - June 19, 2008 - 15:11

the nid will depend on whatever the current node is, it's dynamically changing

Not available

yelvington - June 19, 2008 - 16:27

It's not a global. I think your problem is that $node is not available in the context where the content is being evaluated.

no as the last post says the

openbook - June 19, 2008 - 16:37

no as the last post says the node object is not globally available! It really depends on what page you are trying to load the node object on, I think the easiest way to get the node id is to use the arg() function to find what is in the URL, eg:

for URL www.site.com/node/31

<?php

// return 'node'
print arg(0) ;

// return '31'
print arg(1) ;
?>

This also works for url aliases, as the arg() function uses the unmodified url path.

hope this helps.

for customizing breadcrumbs

vj0914 - June 19, 2008 - 17:18

Thanks all for replies to this post.

the reason I wanna get nid is to customize breadcrumbs on the page,

now I have 3 different page, client-1, client-2, client-3

under each page, I have different content which is clickable. What I want to do is to get nid of current content showing, and based on the nid of

current showing content, I can track back where this content comes from, e.g

Home>>client-1

Home>>client-2

Home>>client-3

this way viewers don't need to click "back" to go back to the previous page.

 
 

Drupal is a registered trademark of Dries Buytaert.