0

How can I print taxonomy name with published node count with a custom link?

eg: Taxonomy term-a (23) Taxonomy term-b (10) Taxonomy term-c (55)

I need to link each of them to a separate page.

I found this below code to print taxonomy term with count. but now how to make to print specific taxonomy term with it's node count? so that I can link it to a page later, so if I repeat the same code with different term ids to have multiple terms with its count.

    <?php
$vid = 23;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$parent = 130;

$terms = taxonomy_get_tree($vid, $parent, -1, 1);
foreach ( $terms as $term ) {
  $count = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid WHERE t.tid = %d AND n.status = %d", $term->tid, 1));
  {
    print '<div>' . l($term->name, "taxonomy/term/$term->tid") . " ($count)"  . '</div>';
  }
}
?>

Can someone please help me?

__HAD TO ANSWER HERE AS I CAN'T AT THE BOTTOM__

This is it!!!

Took a while to write this code... :(

<?php
$tid = 100;
$term = taxonomy_get_term($tid);
$name = $term->name;
$result = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid WHERE t.tid = %d AND n.status = %d", $term->tid, 1));
print '<div><a href="http://example.com/">' . $name. " ($result)" . '</a></div>';
?>

Change: 100 at the second line of above code to your specific taxonomy id, that's it and repeat the same for different tid.

And make sure to change the link at the bottom from (http://example.com/) to your (custom link).

Thanks a lot to all, let me know if this code can be fine tuned further...

  • 1
    A lot of similar questions already available, please have a check drupal.stackexchange.com/… – Anoop Joseph Oct 18 '12 at 12:08
  • actually I looked for it, but could not get what I need. – AnAnD Oct 18 '12 at 12:22
  • I am looking for to print node count for a single taxonomy term id, I could not do that using above code – AnAnD Oct 18 '12 at 12:23
  • did you looked this one , drupal.stackexchange.com/questions/37466/… it allows you to build the same using view – Anoop Joseph Oct 18 '12 at 12:26
  • since I have to link each term + it's count to a custom link I cannot use views, though if am using views I cannot filter the term count by published nodes only + I cannot custom link it :( – AnAnD Oct 18 '12 at 12:29
0

Although it looks like you have come up with a solution, you might want to compare against the Taxonomy List module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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