Display Tags in 3 Columns

If you’re tired of all of the tag clouds you see out there and just want a simple list that you can browse through, then this bit of code is for you. In WordPress, there are convenient functions to list all tags (and categories) outside of The Loop in an unordered list. However, this doesn’t make good use of horizontal space and can get out of hand quickly if you have a lot of tags. But, the advantages of having an alphabetical listing is worth the extra effort.

For this particular example, let’s assume we want to list our tags on the archives index page.

<?php
$tags = get_tags();
$tags_count = count($tags);
$percolumn = ceil($tags_count / 3);

for ($i = 0;$i < $tags_count;$i++):
    if ($i < $percolumn):
    $tag_left .= '
	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .'</a></li>
' . "\n";
    elseif ($i >= $percolumn && $i < $percolumn*2):
    $tag_mid .= '
	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .'</a></li>
' . "\n";
    elseif ($i >= $percolumn*2):
    $tag_right .= '
	<li><a href="'. get_tag_link($tags[$i]->term_id) . '"rel="tag">' . $tags[$i]->name .'</a></li>
' . "\n";
    endif;
endfor;
?>
<ul>
<?php echo $tag_left; ?>
</ul>

<ul>
<?php echo $tag_mid; ?>
</ul>

<ul>
<?php echo $tag_right; ?>
</ul>

The majority of this code was modified from a two column version I found on the WordPress Forums, but since I needed three columns I had to take it one step further. The great thing about this code is that you can adapt it to output the tag RSS feeds by simply adding feed before the rel tag. For example:

$tag_left .= '<li><a href="'. get_tag_link($tags[$i]->term_id) . 'feed "rel="tag">' . $tags[$i]->name .'</a></li>' . "\n";

Here’s the code in practice.

If, for some reason, you really really love tag clouds and you think this code is a waste of time, you may want to check out some best practices for using tag clouds.

5 Responses

  1. This piece of code has been a great help, thanks! It’s hard to find a decent code out there for a simple tag list.. but I do have one question. Is it possible to create a single column, instead of three? I tried, but I’m not that good with coding and all that.

  2. Hmm, I’m having the opposite problem – I’ve installed the code (on a client’s blog) and can’t get it to adapt from one column to 3…looks great on the sample page, just the effect I’m looking for.

  3. @Kim

    Creating a single column is pretty easy and you don’t even have to use the above code. If you want a single column of tags use this:


    wp_tag_cloud('smallest=1&largest=1&unit=em&format=list');

    This will use WP’s tag cloud function but output them all in an unordered list and the same font size.

    @thewebwriter

    What kind of problems are you getting? Is there an error message?

  4. Hey,

    Great code. I was looking for something like this. I actually made a slight change as I’m listing tags on separate page by letter. I also wanted to add in after each tag the number of posts in that tag. I’m just not sure where to add the code to count the number of posts per tag. I was changing the output to something like:

    term_id) . ‘”rel=”tag”>’ . $tags[$i]->name .’ (‘ . $count . ‘)

    But couldn’t get it to read the correct number. Any thoughts?

  5. @Gary,

    To display the count, all you need to do is add this:

    $tags[$i]->count

    Hope that helps!

Leave a Reply