Related Content in Drupal 7 using Views

Submitted by Nicola Rainiero on 2013-05-13 (last updated on 2015-04-25)

After some boring tests and three different types of tutorial that have never been able to work on my site, finally I have found the best compromise for me: a related content based on the taxonomy and placed as a block above the comments field. Here is my Views settings and how to insert the resulting block directly in the node.tpl.php template file.

In my opinion Similar/Relevant content is an appropriate manner for highlighting and linking the own posts. At the same time is a trick for increasing the permanence of visitors in my site!

As usual I started my research from the existent modules for Drupal 7.x and I have found an eloquent answer reading the Comparison of Similar / Relevant content block modules page. However their features didn't satisfied me, because every module doesn't have all the characteristics I need: multilingual support, caching and term base. Actually I would have liked to create a new Views!

An example of Similar/Relevant content
An example

Even in this case there are many tutorials that explain how to create a view block for this purpose, I put the three different types and my notes:

I have followed the third one and at the first attempt I have obtained that I needed. Therefore I have changed some aspects to fit my expectations, below there is my configuration.

Related Content view settings

  • From my_site/admin/structure/views create a new view block (I have selected Table in the Display format sub menu, because its pagination is easier):
    Create a new view block

  • Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
    Add in Contextual filter the Content: Nid item
  • Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
    Add in Relationship the Content: Taxonomy terms on node item

  • Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
    Add in Relationship the Taxonomy term: Content using the proper vocabulary

  • Turn yes Use aggregation
    Look the arrow in the picture
  • If you need to show an image and a title like me, add the proper field and remember to Check off Require this relationship
    Add Image Field
    Add Title field

  • Add Sort Criteria -> Content: Nid. In aggregation settings, select Count. Use relationship as described above and sort descending
    Add in Sort Criteria the Content: Nid item

  • Add Contextual filter -> Content: Nid -> Use the same value of relationship -> Provide default value -> Content ID from URL -> Scroll down and expand "More" then check "Exclude" to remove current node from the view
    Add in Contextual filter the Content: Nid item
  • Add Filter Criteria -> Content: Language if you have a multilingual site -> Use the same value of relationship
    Add in Filter Criteria the Content: Language item
  • Finally fix the Style options for the table
    Style options for the table
  • Click on the Save button in the upper right side and you will have a new view block ready to be placed where you want.
    The final settings overview

Placing the View Block

You have two choice:

  1. If you want just put it in a block region go in the Block menu (my_site/admin/structure/block) and follow the classical instructions.
  2. If you want Placing the contents of a block in any location, discover the "block_delta" name of your block. How? You can determine what this is by visiting the block administration page and editing the block. For example: my_site/admin/structure/block/manage/views/related_content-block/configure

    And overwrite the node template file of your custom theme (node.tpl.php). In my case I created my theme from Bartik and I put the following code above the comments:

    [... original file ...]
    global $language;
    $block = module_invoke('views', 'block_view', 'related_content-block');
    if (($language->language == 'it') && isset($block)) {
    echo "<div style='font-size:20px; font-weight:900;'>Contenuti correlati</div>";
    else {
    echo "<div style='font-size:20px; font-weight:900;'>";
    print render($block['subject']);
    echo "</div>";
    print render($block['content']);
    echo "";
    echo "";
    [... original file ...]
    <?php print render($content['comments']); ?>

(13/06/2013) I have added "isset($block)" in order to check if $block is not empty, otherwise the title "Contenuti correlati" is present everywhere in the Italian section.

Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.