<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yet Another Tech Blog &#187; php</title>
	<atom:link href="http://www.yetanothertechblog.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yetanothertechblog.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 12 Jul 2010 17:49:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Self made tag cloud for WordPress</title>
		<link>http://www.yetanothertechblog.com/2007/07/08/self-made-tag-cloud-for-wordpress/</link>
		<comments>http://www.yetanothertechblog.com/2007/07/08/self-made-tag-cloud-for-wordpress/#comments</comments>
		<pubDate>Sun, 08 Jul 2007 16:37:58 +0000</pubDate>
		<dc:creator>flod</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.yetanothertechblog.com/2007/07/08/self-made-tag-cloud-for-wordpress/</guid>
		<description><![CDATA[In this tutorial I will show you how to create a simple tag cloud with WordPress categories and place it in your sidebar without using external plug-ins or widgets (you can read here an Italian article about the same argument). You can write the following code inside the sidebar.php file in your template, or create [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will show you how to create a simple <strong>tag cloud</strong> with WordPress categories and place it in your sidebar without using external plug-ins or widgets (you can read <a href="http://www.pseudotecnico.org/blog/2006/12/25/wordpress-tag-cloud-per-le-categorie/">here</a> an Italian article about the same argument).</p>
<p>You can write the following code inside the sidebar.php file in your template, or create a new file (<em>tagcloud.php</em>) and include it in your <em>sidebar.php</em>: the second way is better if you have multiple sidebar files.</p>
<p>Let&#8217;s start with the second solution: create a file <em>tagcloud.php</em> and place it in your template folder. This is the code you have to write inside this file (you can download <em>tagcloud.php</em> in text format <a title="tagcloud_php.txt" href="http://www.yetanothertechblog.com/wp-content/uploads/2007/07/tagcloud_php.txt">here</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Extract categories from database</span>
<span style="color: #000088;">$qrystr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT cat_ID, cat_name, category_count from <span style="color: #006699; font-weight: bold;">$wpdb-&gt;categories</span> ORDER BY cat_name&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cats</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qrystr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Find the max number of posts for a single category</span>
<span style="color: #000088;">$max_value</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cats</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cat</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$max_value</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">category_count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$max_value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">category_count</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>  	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// If you want to change the appeareance of the tag cloud (maximum and minimum percentage in font-size, percentage difference between levels), change only these 3 variables</span>
&nbsp;
<span style="color: #000088;">$min_percentage</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$max_percentage</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$percentage_level</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$num_levels</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$max_percentage</span><span style="color: #339933;">-</span><span style="color: #000088;">$min_percentage</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000088;">$percentage_level</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$postgap</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$max_value</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$num_levels</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Display all categories inside a div with id=cloud</span>
<span style="color: #666666; font-style: italic;">// Assign a class to the category (or categories) with more posts</span>
<span style="color: #666666; font-style: italic;">// Do not show categories without posts</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div id=&quot;cloud&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cats</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cat</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">category_count</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$catname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_name</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$catlink</span> <span style="color: #339933;">=</span> get_category_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cat_ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$postcnt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">category_count</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$fontsize_percentage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min_percentage</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$postcnt</span><span style="color: #339933;">/</span><span style="color: #000088;">$postgap</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$percentage_level</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$catlink</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #000088;">$postcnt</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$str_post</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' post&quot;&gt;'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$str_post</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' posts&quot;&gt;'</span><span style="color: #339933;">;</span>  
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fontsize_percentage</span><span style="color: #339933;">==</span><span style="color: #000088;">$max_percentage</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' style=&quot;font-size: '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fontsize_percentage</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'%&quot; class=&quot;top_category&quot; title=&quot;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$postcnt</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_post</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>   
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  	 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' style=&quot;font-size: '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fontsize_percentage</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'%&quot; title=&quot;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$postcnt</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_post</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>  
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$catname</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&lt;/a&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/div&gt;&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Let&#8217;s take a quick look at the code:</p>
<ul>
<li>I make a query on the WordPress database (table &#8220;categories&#8221;): results are ordered by category name and we already have all the data we need thanks to the field &#8220;category_count&#8221;</li>
<li>I check all the results to find the maximum number of posts for a single category. This value is stored using the variable $max_value</li>
<li>I set the maximum and minimum percentages to use as font-size, percentage difference between levels and the number of posts between two levels (this is why I need $max_value)</li>
<li>now I can walk trough the results of the query, generating the html code: I write a simple link, using <em>title</em> to display the number of the posts for each category (so you will see a tooltip), and assign a special class to the top category (or categories)</li>
</ul>
<p>Now change your <em>sidebar.php</em> to include the <em>tagcloud.php</em> file; obviously you must choose the right point to place it <img src='http://www.yetanothertechblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>At this point you should already be able to see the <strong>tag cloud</strong> in your blog: the next step is to style it using css. This is the stylesheet I use for this blog:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #cc00cc;">#cloud</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">dotted</span> <span style="color: #cc00cc;">#E4F0DB</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.5em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.6em</span> <span style="color: #933;">0.5em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Tahoma<span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">justify</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.7em</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #cc00cc;">#cloud</span> a <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#2f63b3</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #cc00cc;">#cloud</span> a<span style="color: #6666ff;">.top_category</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#76b391</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #cc00cc;">#cloud</span> a<span style="color: #6666ff;">.top_category</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#4A5265</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That&#8217;s all <img src='http://www.yetanothertechblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p class="tag_technorati">Technorati Tags: <a href="http://technorati.com/tag/tag+cloud" rel="tag">tag cloud</a>, <a href="http://technorati.com/tag/wordpress" rel="tag"> wordpress</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yetanothertechblog.com/2007/07/08/self-made-tag-cloud-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
