How to List Related Tags on Drupal Taxonomy Pages Using Views

Make sure you check out my previous article How to List Drupal Child Terms on Taxonomy Pages with Views as today I will extend a little bit my previous tutorial and at the end we will add related tags on taxonomy pages using views.

Tools (modules) you will need: Views module and taxonomy module enabled.

Here is the print screen:

Listing related tags on Drupal taxonomy page

Listing related tags on Drupal taxonomy page

Or see live example at Drupal development (or any tag on this blog)

This will work with simple one taxonomy per content type enabled and it will work with multiple taxonomy fields enabled per content type. I'm using two level taxonomy fields - first I have a pre-defined taxonomy fields including child levels, second I have second taxonomy field with tagging enabled in free format. Which basically means following - each article is listed into pre-defined category, like this one goes into Drupal Development, and second tags are added in free format, for example Views, Drupal tutorial... 

In following example we are going to use the power of Views attachments:

Drupal view with attachment

Open your taxonomy/term/% view and add a new attachment. Give it a nice name, Related tags, for example. 

Start by adding following contextual filter:

Content: Has taxonomy term ID (with depth)

  • Depth:0
  • Provide default value: Taxonomy Term ID from URL
  • Load default filter from term page

Relationships:

  • Content: Taxonomy terms on node
  • Require this relationship

Other:

  • Use aggregation: Yes

Format:

  • Format: Unformatted
  • Show: Fields

Fields:

  • Taxonomy term (name)
  • Taxonomy term (tid)
  • Global: Custom

Paste following in Custom field:

<div class="tax-menu">[name] <span class="small"> ([tid])</span></div>

were tax-menu and small are a div classes you can latter use for styling your sub-menu

Sort criteria:

Make sure you remove date, cause it will break things if you have enabled aggregation

  • Taxonomy Term ID, use aggregation Count

Attachment settings:

  • Attach to: Page
  • Attachment position: before

Save your view, and you should be done with technical stuff. Now it is time to apply some CSS. 

Hope this helps! 

Add new comment