Tag Cloud, an add-on for the tags mod

Comments (4)

Be sure to check out the improved tag cloud first!

This is a tag cloud function I whipped up this afternoon. I did this very quickly, so I'm sure it could be improved upon. Copy the function below to your snews.php file, just above the closing tag.

You can change the minimum and maximum percentages if you like. To use, you simply call the function in your index.php like this;


<?php echo get_tagcloud(); ?>

or if you want to call it from inside an article, extra, or page, you insert it like this;

[func]get_tagcloud:|:[/func]

If you're using my tags mod version 1 and want to continue using that, simply replace the highlighted text with ?search_query= instead of tag/

function get_tagcloud() {
    $max_size = 300; // 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 != ''"; 
    $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; 
    $x++;
    }

    $tagslist = explode(", ",$tagslist);
    asort($tagslist);
    $tagslist = array_count_values($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);
    echo '<div class="tagcloud"><p>';
    // loop through the tag array
    foreach ($tagslist as $key => $value) {
        $fontsize = round($min_size + (($value - $min_occur) * $step));
        echo '<a href="'._SITE.'tag/'.urlencode(strtolower($key)).'/" style="font-size: '.$fontsize.'%" title="'.$value.' items tagged with '.$key.'">'.$key.'</a> ';
    }
    echo '</p></div>';
}

Everything is contained in a div with the class of "tagcloud", so you can add that to your stylesheet. Here is a demo of the output from my site

bookmark / share this: Bookmark and Share
rated 5/5 (3 votes)


4 comments

Add a new comment »

Sven Sven said:
Feb 7th, 2009 at 4:09 am

wow, well done Matt !

It looks very good, now I only need more "spare time" to be able to modify my sNews with all your mods ;)

Your mods are useful and really improve sNews, thanks again Matt


Matt Matt said:
Feb 7th, 2009 at 2:39 pm

Thanks Sven, I appreciate the comments from yourself and others, makes it worthwhile.


Philippe Philippe said:
Feb 14th, 2009 at 2:29 am

I'm a fan now.
That one is the ultimate Tag Clod Mod for sNews. ;-)

One thing, i think tags links should not be adjacent, so ine the last echo it needs a | to separate links:
| ';">

Am I wrong?


Matt Matt said:
Feb 14th, 2009 at 7:05 am

Hi Philippe,

Yes, you could do that if you like, I think it might get a little cluttered however, so I just use the css margin attribute to keep the links from crowding each other.

Like this;

.tagcloud  a {
    margin:2px 4px 2px 0;
}



--Matt



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top