Hi

I saw so many drupal site , They can display the count numbers of the term(how many post within this tem) ;

example : term1(38) term2(46) term3(53) ....

How can I display the count of the post within the term ???

Thank you

Comments

jin410’s picture

I just posted something about that here:

http://drupal.org/node/199380

if you read the post, you can see that i'm trying to restyle it but the code itself works.

here it is:


<?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);}
?>

freelylw’s picture

hello

Thank you for your post.

Sorry for I am not a programer, I don't know where I should put these codes on ??? Can you please explain a bit more for where I can use this code ??

Thank you very much!!!

jin410’s picture

make a new block and post the code into it

To make a new block:
so, goto "administer"
then click on "block"
then "add block"

In "block body" copy and paste this into it:


<?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);}
?>

Click on "input format"
then make sure "php code " is selected.

Save the block

You'll have to select the area you want the block to be displayed in (usually right or left)
choose one and click "save blocks"

should work
Just remember to Set the vid to the vocabulary id of the vocabulary you wish to list the terms from.
The code i posted is using vid "2", you might need to change that to what ever number you want displayed.

green monkey’s picture

thank you for this post - it did the trick.

using terms in a block for a new site - of course terms are near empty, but wanted to show the projected direction of site.

now, I won't be upsetting people sending them to blank areas

thanks again

freelylw’s picture

Hello

THANKS for your reply. Can I ask the vocabulary id is the name of the vocabulary ? am I right ? I putted the name of the vocabulary there to replace the '2' , but there is nothing showing on my page. Can you explain a bit more ?

Thank you so much

green monkey’s picture

I'm not real sure what you are asking, but.....

If you are trying to discover the Vocab ID #.

Go to where you would edit the Vocab and click edit.

then look up in the URL box - you will see the Vocab #

hope that helps

spiffyd’s picture

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.

Anonymous’s picture

How would I use this (ie get a list of all terms and the counts) if I only wanted to show those terms associated with a user's blog items?
eg url is ?q=blog/7