An improved tag cloud for sNews 1.7

An improved tag cloud for sNews 1.7

I'm always seeking to improve my hack & mods, and after I created my tag cloud mod add-on, I knew I would run into one particular issue fairly soon... too many damn tags!

I don't know about you, but I want my "popular tags" cloud to contain only the most popular tags, maybe the top 20 or 25 tags only, not the 55+ I already have currently throughout my site, I imagine people running sNews sites with hundreds or thousands of pages could end up with an ungodly beast of a tag cloud without additional controls.

Therefore, I have created an improved tagcloud function, aptly named get_tagcloud2 :)... there are several improvements in this one;

  • You can pass a $limit variable to the function call, then the tag cloud will grab only the top $limit of tags, resort them alphabetically and display them.
  • If you set a limit and the limit is less than the real total of tags, it will generate a "see all tags" type link at the bottom of the cloud. More on this below.
  • The tag cloud will add a font-weight to the tags based on occurrence.

The "see all tags" link will pass the user to the page "tags/", which is either a hard-coded tag page, or you can create a page in the admin section, name it tags, and call the function without a limit set, like this;


Then to call the most popular tags in your sidebar, either use raw php in the index file, such as;

<?php get_tagcloud2("20") ?>

or use the func call in an extra, and the max number of tags you want to show, if there are more tags than the limit, it will create a link to the tags page automatically;


1. The first thing we need to do is add the tagcloud2 function to your snews.php, so BACK-UP that sucker and work off of a copy. Now add both of the functions below (get_tagcloud2 and the helper function cmp) to the bottom of your snews.php file, just before the closing php tag.

function get_tagcloud2($limit) {
    $max_size = 250; // max font size in %
    $min_size = 100; // min font size in %
    $sql = "SELECT keywords_meta FROM "._PRE."articles WHERE date <= NOW() AND published='1' AND visible = 'YES' AND keywords_meta != '' AND category >= '0'"; 
    $result = mysql_query($sql);
    $count = mysql_num_rows($result);
    $x = "1";
    while ($row = mysql_fetch_array($result)) {
        if ($x < $count) {
            $suffix = ", ";
        } else {
            $suffix = "";
        $tagslist .= $row['keywords_meta'].$suffix; 

    $tagslist = explode(", ",$tagslist);
    $total = count($tagslist);
    if ($limit == "") {
    $tagslist = array_count_values($tagslist); 
    $counter = count($tagslist);
    if ($limit != "" && $limit < $total) {
        $more_tags = '<br /><span class="tag_link"><a href="'._SITE.'tags/">'.l('view_more_tags').'</a></span>';
        uasort($tagslist, 'cmp');
        while(count($tagslist) > $limit) {
            $pop = array_pop($tagslist);
    $max_occur = max(array_values($tagslist));
    $min_occur = min(array_values($tagslist));
    $range = $max_occur - $min_occur;
    if ($range == 0) { 
        $range = 1;
    $step = ($max_size - $min_size) / ($range);
    if ($limit == "") {
       echo str_replace("###", $counter, l('tag_cloud_view all'));
    echo '<div class="tagcloud"><p>';
    // loop through the tag array
    $x = 0;
    foreach ($tagslist as $key => $value) {
        $fontsize = round($min_size + (($value - $min_occur) * $step));
        $weight_normal = ($min_size + 15);
        $weight_bold = ($min_size + (($max_size - $min_size) * .6));
        if ($fontsize < $weight_normal) {
            $weight = "300";
        } elseif ($fontsize >= $weight_normal && $fontsize < $weight_bold) {
            $weight = "600";
        } else {
            $weight = "900";
        echo '<a href="'._SITE.'tag/'.urlencode(strtolower($key)).'/" style="font-size: '.$fontsize.'%;font-weight: '.$weight.'" title="'.$value.' items tagged with '.$key.'">'.$key.'</a> ';
    echo $more_tags.'</p></div>';

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    return ($a < $b) ? -1 : 1;

2. OK, now we need to add the hard-coded "tags" page, make the following highlighted changes;

$l['cat_listSEF'] = 'archive,contact,sitemap,tag,tags,

3. now in the function center(), find where you added the url code from the tags mod, and add the "tags" portion highlight below;

global $categorySEF, $subcatSEF, $articleSEF;
    $url = explode('/',clean(cleanXSS($_GET['category']))); // already done if you have the SEO search mode installed
    if ($url[0] == "tag") {
        $tags_query = $url[1];
    if ($url[0] == "tags") {
        $show_tags = $url[1];

4. Now still within the function center(), find the following code from the tags mod, and add the highlighted code below;

case isset($tags_query):
    tagsearch($tags_query); return; break;
case isset($show_tags):
    get_tagcloud2(); return; break;

5. Now open your language file and add the following section, don't change the ### part, that will be replaced with your tag count;

# tags
$l['tag_cloud_view all'] = '<h2>Viewing all ### tags</h2>';
$l['view_more_tags'] = 'view all tags &raquo;';

That should direct the page to show ALL the available tags on your site.



You might like


Well done Matt!
Great addition!
Tanks a lot.

Philippe, that's the standard tag display page, you need a tag after that one, i.e.

<a rel="nofollow" href=""></a>

you're missing the "s", check the link at the bottom of your tag cloud...
look here
<a rel="nofollow" href=""></a>


"or you can create a page in the admin section, name it tag"
I named it tag. You're right.
With an «s« it's working fine.
Have a nice day Matt.

Damn typos, fixed.

May I ask a little question Matt?
Do you know why my tag pin-up can't be searched (no result for pin-up).
Correct spelling is pin-up and not pinup, so search engines doesn't index the pag properly if there's -.

Thanks for giving a chance to see my pin-ups on the SERPS.


Hyphen support in MySQL full-text must be enabled. Or rather hyphen delimiting must be disabled.

If you can't do that, then you'll want to use the second tagsearch function; step 9.

<a href=""></a>

This one is a little slower due to the mysql regex, but the upside is you can use 3 letter tags if you want.

oh ho...
it seems I missed that chapter.
Problem is solved now.
Thanks a lot Matt!

Think I have the same problem (subdomain) as with the archive page.
The view all tags » is NOT working.

PP, that shouldn't be an issue b/c the tags mod is using the GET['category'] from snews.

Did you add a "tags" page by hand, or hard-code it like in step 2?

I forgot to change step 3
My mistake.

No worries PP, step 3 is an add, more-so than a change, as you'll need both bits, "tag" page shows the regular tags and "tags" page shows all tags.

So you need this whole thing when it's all said and done;
$url = explode('/',clean(cleanXSS($_GET['category']))); // already done if you have the SEO search mode installed
    if ($url[0] == "tag") {
        $tags_query = $url[1];
    if ($url[0] == "tags") {
        $show_tags = $url[1];

Great tutorial here and on tags generally. Thanks!


I find very useful this mod but I have some problems with UTF-8, pls help if you can, i have write all the details in snews forum.

Thanks for your mods!!

Hi Sibas,

I will try to have a look at this over the weekend.



Which version of the tagsearch function are you using, the MySQL full-text search, or the REGEXP?

Also, do you have a link that I can take a look at?

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top