Sorry for the simple question, but i've got so far on our hosted Drupal installation without having to write any PHP code, but now I need a tiny snippet.
I have a set of pages of type "A". Pages of type "B" refer to type A pages using a noderef field.
Now I want to add a block at the bottom of the type A pages that lists "All my type B pages". I have built a view that generates the list, it needs the ID of the type A node passed in a parameter.
I have a small chunk of code that will do this when embedded in the page as a block-
<?php
$view_name = 'series'; //name of view
$limit = 5; // number of returns
$view_args = array($my_nodeid);
$view = views_get_view($view_name);
print views_build_view('embed', $view, $view_args, TRUE, $limit);
?>
Now if I test this with a hard-coded node-id it works fine, I just need to find out how to get the id of the current page!
Note my page URLs are aliased, so I can't just pull arg[0] and arg[1] out of the URL.
I found this code-
<?php
global $node;
$my_id= $node->id;
....
?>
But this is not picking up the node id. Surely the node id of the current node is a piece of the page context that will be very frequently used?
Comments
There is no global node, you
There is no global node, you need something like
erk
Hmm.
1) I thought I couldn't do that because my pages are accessed through
aliased URLs like /series/hello+world, so I was guessing i'd get "hello world"
in arg(1).
2) This is a bit tacky if it has to be done in almost every piece of PHP. Is
there a good reason not to have a current page context object, with
everything like nodeID, Title, Author etc. readily to hand?
Anyway, thanks a lot for the pointer, i'll give it a whirl.
Regards: Colin
Even when nodes are aliased
Even when nodes are aliased the arg() function returns the parts of the un-aliased path. So even if you have your node aliased to '/series/hello+world', arg(0) will be 'node' and arg(1) will be the node id. As for point 2 I have done sites where the current node was needed in a lot of pages, I wrote a small module that provided the node context if viewing a page like node/123.