Often in Drupal you may wish to have a block on your page and only show content based on the node you are currently viewing. This article shows you how to get around the tricky aspect of passing a Node ID as an argument, to output your display.
Why use arguments?
Arguments are an excellent piece of functionality inside Drupal. They allow you to show results and extract information based on a particular variable. In Views, you may wish to only show a piece of information on a particular Node ID. You may have a CCK field belonging to a particular node that you wish to show, but only on individual nodes.
field_author is a fictional field. You may want to have a display that says "More from the author: *insert authors name here*".
So to do this you need to setup your view to show the field_author and have your filters set to Node type: Story and Node type: published (if your author field belongs to a Story node). If your author belongs to a page, use Page instead of Story and so on.
Now we add our arguments, add the argument for Node: NID. Use the following settings:
- Action to take if argument not present: Display all values
- Validator: Node (check your content type too)
- Argument type: Node ID
Test it out inside Views
Inside of the views module, enter a node ID of one of your storys/pages - you should see your field (author name) display in the results. In an ideal world, we could display this as a block and then just assign it to a region on the blocks page. But as a block will not take a Node ID as an argument, this will not work. To get this to work, use the following code inside your template file:
- <?php print views_embed_view('view_name', 'default', $node->nid); ?>
In our example, our view name is show_author
- <?php print views_embed_view('show_author', 'default', $node->nid); ?>
This is now outputting the view directly as your "default" display instead of a "block" - you can now embed this into your page.tpl.php file.
Posted by: garry