Get the current node id (Drupal)

  • Posted on: 5 April 2017
  • By: jmu

1. How to get the current node id.

<?php
//get current node id
$nid = 0;
if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
}

$node = node_load($nid);

if ($node) {
   print "Node: $node->nid with title $node->title";
} else {
  print "NODE IS EMPTY";
}

?>

 

2. Distinct whether this page is a CART page for e-commerce website

<?php
if (arg(0) == 'cart') {
   print "This is cart page";
}
?>
English