This recipe shows how to apply the Contextual View idea to create a block that shows news headlines related to the current page being displayed. It assumes you have two node types, which I'll call "primary" (this is the node type that contains the base information) and "news" (this is a node type that defines a story, announcement, or whatever). In our case, the "news" stories are often directly related to one of the "primary" nodes on the site, and we wanted to show all the "latest news" related to each primary node in a block on that node's page.
Here's the recipe:
Here's the recipe:
- Add a CCK "nodereference" field to your "news" type node.
- Use a "Select List" type, with a title like "This news relates to";
- Select the "primary" node type as the "Content Types that can be referenced" (and/or set up a View to restrict which nodes to list further, by user id, taxonomy, or whatever).
- (You might even choose to set this to be visible in the "Display Fields" settings, as this will provide a link back to the "primary" node it relates to.)
- In Views, create a Block view with a title like "latest news", set to display 5 nodes (say, with a more link). Use a List view.
- Select the Node:Title Field (as links), so you get links to the "headlines".
- Filter based on Node Type is "news" (and Published, of course)
- Add the Contextual View Block magic, to tell the view to pull in the news stories that have a reference to the current page! Add the argument: "Node Reference:This news relates to" argument, with "Use Empty Text" as a default. Add the following Argument Code:
if (arg(0) == 'node' && is_numeric(arg(1))) {
$args[0] = arg(1);
}
return $args;
- Sort in reverse chronological order (so latest news goes on top);
- Go to admin/blocks and configure the block to show up wherever you like. Set the block to only display on pages that will display your primary type CCK nodes.
Done. Wherever you have enabled this block, it should show only news stories that are related to (have a reference to) the current page!
No comments:
Post a Comment