I need a function to return the nid
of the current node
that the current user
is watching.
Can you help me?
2 Answers
If you are insisting on a function then you are looking for arg
function.
arg(0)
will return 'node'
and arg(1)
will return node nid
.
Provided you must be inside a node.
[Updated Answer]
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
if ($nid) {
$node = node_load($nid);
/** do something **/
}
}
Alternately
you can get nid
from node object $node->nid
-
Oh,arg is very exiting function,but how to use it?can you give me some example? Aug 8, 2013 at 6:08
-
-
1If you want the node object directly you can use the function
menu_get_object()
, it will save you a few lines of code– wranvaudApr 11, 2016 at 19:44
You can access $node
object. $node->nid
will return you NID of the node.
There is a hook to help you further to achieve something when user is viewing node - hook_node_view().
There are three parameter of the hook - $node
(will return node object), $view_mode
(view mode of the node like full or teaser) & $langcode
(language code for rendering).
You can use this hook in your module & write your logic like -
function MYMODULE_node_view($node, $view_mode, $langcode){//replace your module name with MYMODULE here
if($node->type == 'article' && $view_mode == 'full'){ //if content type is article & user is on full node view
//do domething
}
}