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:

  1. Add a CCK "nodereference" field to your "news" type node.
  2. Use a "Select List" type, with a title like "This news relates to";
  3. 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).
  4. (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.)
  5. 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.
  6. Select the Node:Title Field (as links), so you get links to the "headlines".
  7. Filter based on Node Type is "news" (and Published, of course)
  8. 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;
    
  1. Sort in reverse chronological order (so latest news goes on top);
  2. 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!

Comments

FiNeX’s picture

Is this possible on views 2 in Drupal 6?

okday’s picture

subscribing

okday’s picture

Have you find a solution to get it working with views2?

lelizondo’s picture

grantkruger’s picture

Here's a discussion around having a block with related content that covers much of the above in some detail. It starts with a proposed solution, undergoes some refinement in the comments, and then finishes up with a final best answer.

minhduclol’s picture

I have one question: how could I display a "new" or "hot" gif file following the title which has publish day <3 days ?