Skip to content

Instantly share code, notes, and snippets.

@tung
Created September 23, 2011 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tung/1236951 to your computer and use it in GitHub Desktop.
Save tung/1236951 to your computer and use it in GitHub Desktop.
Tag list block for Drupal 6.x
<?php
/**
* Abstraction-breaking custom tag lister.
* This block lists all tags under the given taxonomy vocabulary,
* in order of frequency, and then name. The tags are hyperlinked.
*/
$vocabulary_id = 8;
$cutoff = 15; // don't show results that don't pass the threshold
$threshold = 5; // after cutoff, don't show tags with counts less than this
$tag_query = "SELECT td.tid, td.name, COUNT(DISTINCT tn.nid) as num " .
"FROM term_data td, " .
" term_node tn " .
"WHERE td.vid = %d AND " .
" td.tid = tn.tid " .
"GROUP BY td.tid, td.name " .
"ORDER BY num DESC, td.name ASC ";
$result = db_query($tag_query, $vocabulary_id);
if ($result) {
print "<div class=\"item-list\">";
print "<ul>";
$results_so_far = 0;
while ($row = db_fetch_object($result)) {
$results_so_far++;
if ($results_so_far > $cutoff && $row->num <= $threshold)
break;
print "<li>" .
"<a href=\"/site/taxonomy/term/" . $row->tid . "\">" .
$row->name .
"</a>" .
" (" . $row->num . ")" .
"</li>";
}
print "</ul>";
print "</div>";
} else {
print "Tags unavailable.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment