Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dfennell on
Hi all,
Does anyone know how I can find out what the node id that corresponds to a specific Drupal path?
I've been searching for quite a while for an answer to this and I'm coming up empty, it seems as though there should be a fairly simple way to do this (with node_load, I was thinking, but, I've had little luck with that), but, as I said, no luck!
Thanks.
(I suppose not all paths will correspond to nodes, but, in my case the paths I am interested in will be... I think... I'm pretty new to this stuff)
Comments
=-=
go to administer -> content
filter for the node you want
hover over the title link
there you will find the nid
Thanks, but I meant
Thanks, but I meant programmatically from within a drupal module...
So do you mean a path or an
So do you mean a path or an alias? Nodes always have paths of the form node/{nid} where {nid} is the node id. If you mean looking up the path associated with an alias see http://api.drupal.org/api/function/drupal_lookup_path/6
An alias, I suppose...
I must mean an alias... I'm pulling the aliases from the {menu} table in my drupal database, and some of the time the paths/aliases are in the form /node/{nid}, but, most of the time they're just the aliases.
I've tried the function you've suggested, but, it doesn't seem to be working for me.
Here is my code:
$found_path->path certainly contains an alias, but I am still getting the 'No system path found.' message. (I'm running Drupal 5.x)
Thanks for the help! I feel silly having so much trouble with what should be a very simple matter.
If you want to get the node
If you want to get the node id of the aliased path in the fly, you can use
It will return something ie: node/3, which 3 is the nid if. But if you want to get the nid of the current state path, arg(1) will always be a node id, assuming you are in the node page.
I think my issue may actually
I think my issue may actually be elsewhere in my code... I've been testing on admin/* pages, which are perhaps not nodes at all... both of these methods will likely work on an actual nodes.
Sorry for the trouble, this will be very useful when I'm actually testing on real nodes!
Thanks! :P
Correct, paths under admin
Correct, paths under admin are not typically nodes.
Admin pages are never nodes.
Admin pages are never nodes. Nodes are a specific type of data in Drupal. They really form the core of the Drupal system. Nodes are data that users add, using the 'create content' menu. Pages generated programatically are not nodes (unless someone configures a module to generate nodes, though I've not seen such a module, one could and may very well exist). There are other types of user generated data that are also not nodes, for example private messages in the privatemsg module generates data that isn't nodes, but these are more of an exception than a rule. For the most part, user generated content will be in the form of a node.
Try this: global
Try this:
global $base_url;
$url= "http://www.domain.com/baby/1-week-pregnant";
$alias = str_ireplace("$base_url/",'',$url);
$normal_path=drupal_get_normal_path($alias);
$nid=str_ireplace("node/",'',$normal_path);
echo ("nid=$nid");
It is easy to useif ( arg(0)
For the current path it is easier to use