This phpsnippet (http://drupal.org/node/23415#comment-60089) shows a nice way to display the terms of a particular vocabulary.

Could someone please help with code for the following?

1.) How can I show *just* a list of the vocabulary's terms- that is, without the count, last updated and the 'no content' info
2.) Similarly show *just* the sub-terms of a term?

Thanks in advance

Comments

nevets’s picture

This snippet will just show the term names (linked to page list content with term) for a vocabulary

<?php
$vocabulary_id = 1;
$result = db_query("SELECT d.tid, d.name FROM {term_data} d WHERE d.vid = $vocabulary_id 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);
}
print theme('item_list', $items);
?>
nisguy’s picture

Greetings,
I'm trying to setup a list of sub-terms within a parent term and show the number# of nodes contained within (parentheses). For example, say my vocabulary is 'Food' and I have a term of 'Fruit', which has sub-terms called 'apples', 'oranges', and 'bananas'.

I want my list to appear like this if 'apples' had 3 nodes, ...etc:

All Fruit

The snippet above is great, I just need to add the (#) feature to the first snippet. Has anyone done this or can help?

nevets’s picture

This script will just show the sub-terms of a term (specified by setting $tid).

$tid = 6;
$items = array();
$children = taxonomy_get_children($tid);
foreach ( $children as $child ) {
$items[] = l($child->name, 'taxonomy/term/'. $child->tid);
}
print theme('item_list', $items);
venkat-rk’s picture

Thanks, nevets. As always, you are kind to share these code snippets on the forums. They work great. I will find some time to add these to the handbook.

By the way, I had to add <?php> and ?> at the beginning and end of the above code to make it work.

takien’s picture

Hi there...
where i should paste that code in .. ?
i'm using drupal 5.3
help please..

nevets’s picture

You can either place the code in a custom block or in a node, in either case you will need the <?php and ?>tags around the code as mentioned above.

summit’s picture

Hi,

How can you show the nodes in the list (title-body) instead of count of nodes?
Please assist,
thanks in advance,
greetings, Martijn

jimbop’s picture

Something like the following should work:

If using CCK... Where $options is an array of content types as defined in the cck table (i.e. for content type 'notice' this would be 'content_type_notice'):

foreach ($options as $type => $link) {
  $result = db_query("
    SELECT 
      node.title
    FROM node RIGHT JOIN $type USING (nid)"
  );
  while ($result_row = db_fetch_object($result)) {
    print $result_row->title.'<br />'; //or whatever output you want
  }
}

Mind you, you're probably better off using the views module for this. Create a view there and call it manually as described in http://drupal.org/node/48816.