Related Content in Drupal 7 using Views
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 |
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:
- Adding a related content view in Drupal 7, the instructions are easy and the comments are useful but doesn't work.
- Drupal Related Posts Blocks in Drupal 7, although you have to put some PHP code, it is clear and simple, but doesn't work.
- List Related/Similar Nodes using Views 3, easy and well documented, it works well.
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):
- Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
- Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
- Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
- Turn yes Use aggregation
- 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 Sort Criteria -> Content: Nid. In aggregation settings, select Count. Use relationship as described above and sort descending
- 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 Filter Criteria -> Content: Language if you have a multilingual site -> Use the same value of relationship
- Finally fix the 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.
Placing the View Block
You have two choice:
- 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.
- 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 ...]
<?php
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.
Add new comment