Get a node ID from the URL in Drupal

Getting a node ID from the URL in Drupal is very easy just use the following snippet.

if (arg(0) == 'node') {
$nid = arg(1);
}

If you need to get the node object, then you can use the following code:

if ($node = menu_get_object()) {
// Use the nodes object for various process
}
Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.