Community & Support

How do I display all taxonomy catagories in one block?

Well, I guess the topic says it all. I'd like to show every taxonomy category my Drupal installation (4.6.6) features in one single block. I'm going for the classic "WordPress-look" in the sense that I want an overview of every category I use clearly visible in the sidebar (check out an Ubuntu blog and the Linux and Open Source Blog for examples).

Now, I know there are a few third party modules available from drupal.org but none of them really suits me:

  • My site is a single user blog which does not carter for any end-user logins. Hence, Taxonomy Menu is out of the question (moreover, it doesn't show the categories in a single block).
  • I'm using PostgreSQL so I can't use Taxonomy Block neither - besides, having to populate the database for what I'm trying to achieve seems to be quite an overkill.

I'd appreciate any suggestions or pointers to any resources I might have missed out.

Comments

You could use a php snippet

You could use a php snippet in a custom block, perhaps one like "Categories block as in 4.5" > http://drupal.org/node/21390

<?php
if (user_access('access content')) {
 
$result = db_query("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE n.status = 1 GROUP BY d.tid, d.name ORDER BY updated DESC, d.name");
 
$items = array();
  while (
$category = db_fetch_object($result)) {
   
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .'<br />'. t('%time ago', array('%time' => format_interval(time() - $category->updated)));
  }
  return
theme('item_list', $items);
}
?>

This gives you a list of all categorie terms, the number of nodes in each term and the time since the last update, ordered by the latest change.

I am no php or sql wizard but I guess you can change this to give you just the list of terms ordered alphabetically by changing ordered by to just d.name , ripping out the $categorie count stuff and the new line with %time ago.

<?php
if (user_access('access content')) {
 
$result = db_query("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE n.status = 1 GROUP BY d.tid, d.name ORDER BY d.name");
 
$items = array();
  while (
$category = db_fetch_object($result)) {
   
$items[] = l($category->name ,' taxonomy/term/'. $category->tid) ;
  }
  return
theme('item_list', $items);
}
?>

There are others snippets like "Display a list of term titles(x) from a specific vocabulary with the number of nodes in each term" > http://drupal.org/node/31536 that you could take a look at.

Thanks

Gabriella,

tack så mycket - jag tar en titt på kod respektive länkar så fort jag får möjlighet!

(for the Swedish impaired - I'll give a brief summary of the final solution to the problem as soon as I can present some hard facts).

Kind regards,
peter

Vassego'

Alltid kul att se en landsman här - speciellt en som länkar till linux relaterade siter... ;-)

OT: Swedish chit-chat

Hm. Hade jag fått bestämma hade sajten varit AmigaOS-relaterad - Linux är alldeles för mainstream (och hårdvarukrävande) för mig :). Dock brukar det väcka folks uppmärksamhet, så jag ber om ursäkt för att jag tar till sådana fula knep :).

Hursomhafver - tack för hjälpen. Är databasanropet PostgreSQL-kompatibelt tror jag att det skall fungera galant (har inte hunnit titta på det riktigt ännu, är mitt i säsongsavslutningen av "24" och jag har snart inte kvar några naglar att bita upp/av :).

(for our English speaking audience - this is totally off topic, apart from me commending the person helping me out - kudos :)

More swedish chit-chat

Men, tror du inte att du väckt lite mer uppmärksamhet - och nyfikenhet - om du hade använt dig av AmigaOS istället ;-)

Tyvärr hade jag flyttat hemifrån då min lillebror började leka med datorer bla Amiga, så jag har inte ens sett en slik. Det tog några år innan jag själv fick anledning att klappa en dator/pc. Och efter att ha tampats med windows tycktes det mig som att "skåda ljuset" när jag fick prova unix/linux. Vem vet vad som hade hänt om jag fått leka med en Amiga..

Nåja, i brist på Amigor att leka med så har jag laddat hem en "Kororaa Xgl Live CD" för att prova att snurra runt min desktop lite.. Nej, fullt så nerdig är jag nog inte, men ibland man gör ju vad som helst för att skjuta upp saker som att ta tag i disken och släpa fram dammsugaren. Vilket i sig är något överdrivet, vi har ju alltid imorgon..
TOMORROW:
One of the greatest labour-saving devices of today.

Problem fixed, solution (in English)

[swedish off-topic discussion]
Mer uppmärksamhet och nyfikenhet? Jo - kanske :). Tyvärr har så många människor förutfattade meningar vad gäller Amiga-datorer att de inte är mottagliga för konkreta fakta.

Tråkigt att höra att du inte själv fått insyn i AmigaOS - även om det är trevligt att konstatera att du iallafall sållar dig till Linux-folkets halvalternativa skara :) (jag antar att Kororaa Xgl är en i raden av dessa miljoner linuxvarianter).

Disk och tvätt eller inte så är iallafall taxonomy-problemet löst, så jag tar det på utrikiska :).
[/swedish off topic discussion]

The code you provided worked out really well on Drupal 4.6.6! To my reflief, the database query function also seems to work like a charm with PostgreSQL! There is, however, a bug in the latter example:

<?php
$items
[] = l($category->name ,' taxonomy/term/'. $category->tid) ;
?>

For anyone wanting to duplicate my effort, remove the superfluent space trailing " taxonomy/term" as it will screw up the generated URLs. Other than that, the code seems to be a good base for anyone wanting to write a proper taxonomy overview module (which should, in my opinion, be an official and essential part of Drupal).

Gabriella, again - thank you for helping me out. I promise I'll be a happy camper for the rest of the evening! :)

Hälsningar från västkusten,
peter

Oops, sorry about the extra space there..

Alltid trevligt att kunna hjälpa till, vi på Sveriges framsida får ju försöka hålla ihop ;-)

I've been looking everywhere for this!

Thank you!

Views is way complex, and taxonomy_block makes a block of nodes, not a block of categories and tagadelic puts it on a page, not a block.

I was about to give up on what should be a very basic feature and the answer given here is so simple!

For the benefit of others, here's my version which shows categories and number of times used, sorted by number of times used, with a limit of 30 entries:

<?php
if (user_access('access content')) {
 
$result = db_query("SELECT d.tid, d.name, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE n.status = 1 GROUP BY d.tid, d.name ORDER BY count DESC, d.name LIMIT 30");
 
$items = array();
  while (
$category = db_fetch_object($result)) {
   
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid);
  }
  return
theme('item_list', $items);
}
?>

tagadelic page in a block

Does tagadelic have a block that will display all of my vocabularies like on the /tagadelic page? The tagadelic blocks I have only display one vocabulary per block. Is it possible that I had a block like that and it was deleted?

Thank you

nobody click here