2

I'm using a View to display all the items from a taxonomy term list I've setup. The taxonomy term is used as a field in my nodes. That's all fine.

Now, I created another view called 'all categories' that displays all the taxonomy terms (each term is only displayed once). When a user clicks the term they automatically go to the term page and see the nodes that are using that term.

The problem is that some of the terms haven't yet been used by any nodes so when the user clicks the term name for those it just takes them to a page that says [name] at the top with no other information. I'd rather just hide any categories that have no associated nodes, but I don't know how to do it. I'd also be open to other suggestions for alleviating this issue.

Note: if a term DOES have nodes associated with it and you click the term it takes you to a page with the term as the title and the associated nodes listed below the title.

Here's the settings I have for the 'All categories' view:

Show: Fields
Fields: Taxonomy term: Name (link this field to its taxonomy term page)
Filter Criteria: Taxonomy vocabulary: Machine name (=Category)
4

Easiest way is to add a relationship in the view to the node type that contains the taxonomy term reference field and make it mandatory. This will force Views to only return the taxonomy terms that are present on a node.

  1. Edit your view
  2. Expand "advanced" (if its not already expanded)
  3. Click "Add" beside Relationships
  4. Click "Taxonomy term: Representative node"
  5. Click "Require this relationship"
  6. Click "Apply"

Note: I used the representative node vs the content with term option for performance reasons, the former only retrieves a single node for each term vs matching every term to every node

3
  • I test your solution with my vocabulary and don't work as expected – Adrian Cid Almaguer Nov 25 '15 at 4:55
  • @Adrian Cid Almaguer - this worked well for me as far as I can tell. How didn't it work for you? – Sage Nov 25 '15 at 6:07
  • @Sage I see now the reason, I have the i18n module and maybe with this module is necesary other configuration. You know it Jason Reid? – Adrian Cid Almaguer Nov 25 '15 at 12:22
4

Go to the Advanced Settings in the view and click in the Theme: Information

enter image description here

Search the Field Taxonomy term: Name (ID: name) section and select a name, in my case was views-view-field--temp.tpl.php

Go to the folder YOUR_SITE/sites/all/modules/views/theme and copy to your theme folder the file views-view-field.tpl.php and rename it to views-view-field--temp.tpl.php

Edit your new file views-view-field--temp.tpl.php and put inside only this code to eliminate the link:

The taxonomy_select_nodes() function will return an array of nids matching the query.

<?php

if(count(taxonomy_select_nodes($row->tid)) > 0) {
  print $output; 
} else {
  print $row->taxonomy_term_data_name;
}

?>

In the View go to the bottom of the Information window and click Rescan template files and click OK, then save your View.

enter image description here

And go to see your result:

My example:

enter image description here

If you don't want to show the taxonomy items without associated nodes use this code:

<?php

if(count(taxonomy_select_nodes($row->tid)) > 0) {
  print $output; 
}

?>

And your output will be this:

enter image description here

PS: You must copy your .tpl file to the folder of your active theme, then rename in this folder.

2
  • Thanks I appreciate your suggestions. I'm not I think naming my tpl file correctly because when it rescans it doesn't show a new template in the list and it also doesn't change my view. – Sage Nov 25 '15 at 6:08
  • @Sage you must copy your .tpl to the folder of your active theme, then rename in this folder. – Adrian Cid Almaguer Nov 25 '15 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.