I wasn't sure how to word this at all but here goes...

I created a vocabulary word "topics" for my blog and created several terms associated with the vocab.

I'm using the following code (which i put into a block) to display all the terms in "topics" and number of post in each term.


<?php

$vid = 2;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) { 
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid)); 
    $items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
}
if ( count($items) ) {  print theme('item_list', $items);}
?>

my problem is that i want to create a rollover for the list but the number of post is outside the "a href" tag
example:


<a href="/link-to-topic">topic name</a>(#-of-post)

i need it to be more link this:


<a href="/link-to-topic">topic name (#-of-post)</a>

any ideas?

i am a novice at this so any help will be greatly appreciated.

TIA

Comments

mcjim’s picture

Try changing $items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
to $items[] = l($term->name . " ($count)", "taxonomy/term/$term->tid");

See http://api.drupal.org/api/function/l/5

Let me know if it works!

jin410’s picture

Hey, thanks for the reply

The code you posted worked perfect

Would you know how to add a tag around the count?

this is what the output code look like right now:

<a href="/topic/broadway">broadway (1)</a>

and this is what i'd like it to be:

<a href="/topic/broadway">broadway <span id="blue">(1)</span></a>

any ideas?

thanks again

mcjim’s picture

I can't think of anything straight-forward, I'm afraid. Nothing that doesn't involve str_replace, anyway.

Try this:

<?php
$vid = 2;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$items = array();
$terms = taxonomy_get_tree($vid);
foreach ($terms as $term) { 
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
    $item = l($term->name . " ($count)", "taxonomy/term/$term->tid");
    $items[] = str_replace("(" . $count . ")", "<span class='count'>(" . $count . ")</span>", $item);
}
if (count($items)) {print theme('item_list', $items);}
?>

You'll need the span to have a class, not an id (as there are potentially more than one of them).
Also, best to avoid using a colour as an id or class as it may not always stay that colour (you may not always want it blue!).

jin410’s picture

fantastic!!

works like a charm.
I noticed if i set the span to use a "class", the color will not work
but if i switch it to an "id" the color displays, at least on firefox

Thanks again

spiffyd’s picture

Okay I commented on this in another thread here: http://drupal.org/node/199374

Nice, this works. But can any one take this to the next level and... integrate this into the Menu module if one chooses to display vocabulary as the options and its terms as its children (drop down items)?

I think this "count display" option should be native to Drupal or be available in a module!

I'm more of a designer and not a code junkie, so I'm afraid I can't help much.

Looks like someone may want to turn these discussions into a single thread as well.

cratos’s picture

This is a very useful code, but i'm trying to order the results by COUNT.

From what i see the function taxonomy_get_tree($vid) just gets the results dosnt look to anything else and in de Query is just to get the count...

Can someone give me some orientations??.

Thanks in advance