Embedding a 'Related Content' block in your Drupal nodes

Many of my favorite websites offer a nice little feature, immediately following the body of the page, that highlights 3-5 "possibly related" stories or blog posts. I wanted to do this on OSC and some other sites, but found that it's difficult to add regions inside of nodes—the closest I could get with the default theme/block behavior is to have it appear after comment section, which is too far down the page to be relevant.

I decided to use the Featured Content module to create my blocks, as it offers a good amount of customization as to what kind of algorithms it uses to find related nodes... performance considerations aside. There are other ways to go about creating lists of related nodes, but this was quick and easy.

Adapting a solution I found here, I created a simple function inside my template.php file that allowed me to print a block from inside my node.tpl.php template.

Inside template.php:

 * Helper function for retrieving block code for insertion into templates.
 * @see http://drupal.org/node/753516#comment-2769068
function osc_block_retrieve($module, $delta) {
$block = (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
theme('block', $block);

Inside node.tpl.php:

<?php if ($page): ?>

    <div class="block-in-node">
      <?php print osc_block_retrieve('featured_content', '1'); ?>
  <?php endif; ?>

I chose to rank related nodes first by similar taxonomy terms, then by how many views the node received (I'm using the statistics module on this site).

Alternatively, you could do one of the following to accomplish the same kind of thing:

  • Set up a region inside your nodes, in the node.tpl.php file. This seems to be a little burdensome, though, unless you're planning on doing many different things inside said region(s).
  • Use the Panels module to add blocks inside of nodes, or in a different kind of page layout.


Zuuperman's picture

You also could use Display Suite / Node Displays

Gerard McGarry's picture

I did exactly the same thing on my Unreality Shout site, except I used the Similar Entries plugin - it works like a charm every time. I wonder if this kind of integration will be easier with Drupal 7?

Wim Mostrey's picture

On sites that use ApacheSolr I can definitely recommend the customizable content recommendation blocks it is able to generate, all in terms of performance, usability and reliability of results.

Open Source Catholic's picture

Solr is good all around... unfortunately I can't use it on this site, until I find a way to get a little money for a VPS.

Advancing the faith.

Idan Arbel's picture

Views attach could work as well, embed a view in a node.

Open Source Catholic's picture

That's another great option - in fact, if you have a views-provided block, you could simply do a

print views_embed_view('view_name', 'block_1');

Advancing the faith.

Open Source Catholic's picture

Advancing the faith.

Jan Tomka's picture

If you want to give the user full control over what is related content to a node, like RelatedContent module does, you might want to create a vocabulary, whose terms would represent the related content groups. The nodes sharing a term from that vocabulary would be considered related.


Tschet's picture

Good solution. I've used Views Field to insert, but your solution could give me an interesting alternative.