29 June, 2021 seen 6,965In this article I will show how one (perhaps you) can build a Drupal views distance filter by using Drupal, Views and…
Today I will show you, how you can create a list with related items for your Drupal site using taxonomy terms and Drupal views. At first you will need you actual content with taxonomy terms on it. If you are not familiar how to add taxonomy terms to your Drupal site, you might want to read this guide on that.
I presume you have your content set up, it's tagged with different terms. Now lets add a block with related items on that.
Related nodes based on taxonomy terms
In above screen you can see an example with node page and related nodes in block. You can see a live example as well here.
This is achieved by using views and placing it on themes region.
Related nodes by taxonomy term using views
Create a new content based view - call it Related Items (or so) add title, image and body fields to it. We will need to use 2 contextual filters. Nid and Has taxonomy term id.
Configure your nid filter as following:
- Provide default value: Content ID from URL
- Under More tab - Check next to Exclude
Configure your Has taxonomy term id as following:
- Provide default value: Taxonomy term ID from URL
- Check next to: Load default filter from node page, that's good for related taxonomy block
- Multiple value handling: Filter to items that share any value
- Check next to Reduce Duplicates
- Under more tab check: Allow multiple values and Allow multiple values to work together.
That's it. Save your view (be sure you have created a block not page view)
Related items block view
Now you just need to add block you just created to your sites theme region.
It is up to you have you will decide to theme your related items block. Sky is the limit for you creative work. You can add author field, date field, rating field.. so do you can add some filters to show most viewed related nodes content. In this example I gave to you, is just a basic to help you to get started for showing related item list on your Drupal powered site.
N.B. On my example Live preview from views page is not working, but it actually works when added to region in your theme.
Hope this tutorial helped to build your related nodes block, in case of any questions, please feel free to comment or contact me. I'll do my best to help you out.