10

Is there a way to suppress the output of nodes on taxonomy pages, without having to install a module (i.e. just in template.php)?

Nodes tagged with a taxonomy term appear on the "Taxonomy Index page" (e.g. yoursite.com/taxonomy/term/[id]).

New in Drupal 7, Taxonomy Index pages can have fields attached to them. However, if you try to over ride the taxonomy page with a View (for example, to control how the nodes are displayed), the fields on the Taxonomy Index pages will not appear.

I have therefore placed my Taxonomy View as block on the Taxonomy Index page. That way, I can control how the nodes appear and still have the Taxonomy Index page fields.

Because of this, the taxonomy page is still outputting its nodes. I can hide them by setting the teaser of the nodes to hidden, but it still loads up the nodes (and therefore any CSS/Jquery attached to nodes even though the nodes aren't being displayed). How can I suppress the nodes from appearing, ideally from inside of template.php?

1
  • Why do you not want to install a module? Some things are easier to achieve with modules. Jun 20, 2012 at 19:07

5 Answers 5

5

I know, that wanted a no-module solution, but in Drupal 7, there is a module that does exactly what you need: Disable Term Node Listings

It lets you choose per Vocabulary, if you want to disable the output of the term listing.

1
8

Solution from http://www.wardontheweb.com/remove-node-lists-from-taxonomy-pages-in-drupal-7/ works for me well:

function THEMENAME_preprocess_page(&$vars){
   if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))){
     unset($vars['page']['content']['system_main']['nodes']);
     unset($vars['page']['content']['system_main']['pager']);
     unset($vars['page']['content']['system_main']['no_content']);
   }
}

Note: you should reset your site's cache.

3
  • NOTE: this will disable the node listing for any taxonomy term pages on the site.
    – Duncanmoo
    Nov 18, 2013 at 13:48
  • @Peter Lozovitskiy I do this and faced with "Notice: Use of undefined constant ‘taxonomy’ " error!
    – shekoufeh
    Jun 14, 2015 at 5:02
  • @shekoufeh, I've corrected the code (replaced commas) try this code now. Jun 14, 2015 at 7:26
2

You have to remove the nodes from the $page variable.

In template.php you can do it like this:

function MY_THEME_preprocess_page(&$variables) {
  if(arg(0) == "taxonomy" && arg(1) == "term") {
    $variables['page']['content']['system_main']['nodes'] = null;
  }
}

In your page.tpl.php use this code:

if(arg(0) == "taxonomy" && arg(1) == "term"){
  $page['content']['system_main']['nodes'] = null;
}

This removes the nodes from all vocabularies and terms. If you want to remove them only on certain terms or all terms from a vocabulary use arg(2) which contains the tid to load the term and then update the if statement.

1
  • It works but still leaves the pager on. That means that nodes are being loaded and waste the server resourses. Anyway, thanks for the tip, I'm already using your method :) But theoretically a better one still exists.
    – int_ua
    May 17, 2012 at 17:32
0

You can do practically anything in template.php, but that's not recommended. The point of using a CMS, is that you separate data, behavior and appearance. The template.php file is part of the theming layer, and you should use it to adjust the visual style of your site.

If you want to adjust the behavior of a site (in this case: control which items are shown in a certain list) you need to change the behavior, so you install and configure modules. If you want to build custom lists of content, the Views module is exactly what you need.

In your case (if I understand correctly) you want the taxonomy term's fields to be displayed above the list of nodes tagged with that term. You can re-create that in your view by creating a views block that takes the term ID from the URL and displays the info you need.

4
  • Thanks for the reply! I am already using Views to override the taxonomy page. However, I am using the Views Override as block instead of a page, as I need to keep the fields on taxonomy page visible. The only problem is that taxonomy page still loads the nodes, which seems unnecessary.
    – big_smile
    Jun 18, 2011 at 14:03
  • I don't understand what you mean with "the fields on taxonomy page", but hiding the page content and replacing it with a block does not sounds like the correct solution. If you just override the taxonomy page with a regular page view, exactly which functionality are you missing? (Maybe you can update your original question, and I'll improve my answer accordingly.) Jun 18, 2011 at 19:30
  • Answer updated as well. Jun 21, 2011 at 7:55
  • Unfortunately, your answer doesn't seem to work. If you try to bring taxonomy fields into a view, it will display the contents of every single field (and not those that just belong to the specific page being viewed).
    – big_smile
    Jun 25, 2011 at 18:17
0

I was using CSS to hide the taxonomy stuff but setting their display to custom and blanking that seems like a better option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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