This is an update to my original post for related posts blocks in Drupal 6.
Created a block of links for related posts is a useful way to keep visitors engaged in your website and continue reading. This method is specifically for showing other nodes with the same taxonomy terms, but it can be modified for other fields.
Requirements:
- Drupal 7+
- Views 3+
- Some PHP experience
A note about taxonomy terms in Drupal 7:
Drupal 7 stores taxonomy terms as a field. In my example, I have a field name called “Topics” for my blog entries with the machine name “field_topics”. You’ll need to change “field_topics” with your own field name.
Create a Block and Add Contextual Filters
Content: Has taxonomy term ID
- When the filter value is NOT available: Provide default value: PHP Code
- PHP Contexual Filter Code:
$node = node_load(arg(1)); if(isset($node->field_topics)) { foreach($node->field_topics['und'] as $term) { $terms[] = $term['tid']; } return implode('+', $terms); } else { return; } ?>
Make sure you change field_topics with your own field name.
- Check “Reduce Duplicates”
- Under “More”, check “Allow multiple values”.
Exclude the current node
- Add another filter for “Content: Nid”
- When the filter value is NOT available: Provide default value
- Type: Raw value from URL
- Path Component: 2
- Under “More”, check “Exclude”
Save your changes. Place your block anywhere on the node
Go celebrate.
6 replies on “Drupal Related Posts Blocks in Drupal 7”
sorry. I,m doing Related Posts Blocks in Drupal 7, I want to insert images before title as list item?
You would add an “image” field under the “Fields” section and arrange it before the “Title”
Excellent post, just what I needed.
Just one question, Is there a way to limit how many related posts are shown?
Under the “Pager” section (See screenshot), you can specify how many items you want. In my example, I have 5 items.
Hah, can’t believe I didn’t see that, thanks!
Thanks for this post. However the images do not show, due to broken links. Can you please update them