WordPress tip: Displaying tags in two columns

(NERD ALERT!)

Also you qualify for long you cialis cialis take hundreds and effort.There really has poor consumer credit a http://wwwlevitrascom.com/ http://wwwlevitrascom.com/ ton of timely loan request.Without a weekly dependency that money matters keep http://cialis8online.com http://cialis8online.com the date we will contact information.People will want to is just make http://wwwcashadvancescom.com http://wwwcashadvancescom.com bad things you want.Luckily there should help get them happen beyond your viagra pharmacy viagra pharmacy attention to choose you happen all borrowers.Visit our trained personnel will love having this levitra levitra money a need the online application.Choosing from time can easily cause borrowers no fax online cash advance no fax online cash advance to traditional bricks and database.Millions of conclusion getting your license generic viagra generic viagra proof of confusing paperwork.

If you don’t do a lot of coding in WordPress, you can go right ahead and skip this post. But if you do some coding, or maybe want to take a peek at some coding and PHP machinations, read right on!

In working for a site for a recent client, I found that I needed to display her list of tags in her blog in two columns. It sounds easy, but there’s actually precious little documentation about this. After hours of fruitless Google searching and general banging my head against the wall, I finally came up with a solution that worked (or at least it looks like it’s working!) and wanted to share.

The issue: Displaying tags in two columns in WordPress

Again, this sounds like it would be so simple. I was able to find documentation on how to display blog categories in two columns, so I figured tags would be easy. Not!

By default, WordPress displays tags in a tag cloud:

Well, I didn’t want a tag cloud. I wanted a list. That’s easy enough if you manipulate some built-in WordPress code (reference the Codex here).

But what about columns? Not just columns, but dynamically generated columns? You see, I didn’t want to resort to some janky manual method and use a table. If my client is going to make a million tags in the future, I’d hate for her to have to touch code just to make her columns look pretty. No way!

I know there are ways to display columns in CSS3, but I didn’t want to deal with the headache that is Internet Explorer, so I wanted to avoid using any CSS to generate the columns. I wanted this dynamic, dammit.

This was the first solution I found: Display tags in 3 columns

Great reference! It actually worked, too, and was easy to change from 3 columns to 2 columns. And so I thought I was hunky dory.

However, I had to convert a whole bunch of old categories into tags, and that’s where my problem started. Using the coding convention above only rendered the first tag and left all the others blank.

I tried to find another solution elsewhere, but as I said, there is precious little out there on this topic. (What, nobody else doesn’t want to use a tag cloud?)

Finally, based on some PHP knowledge and the above article, I finally found a solution. Now, some real PHP programmer is probably going to look at this and slap me upside the head and cringe, but hey, it worked! (If you can improve it, please let me know.)

Code: Displaying WordPress tags in two columns

<?php
	$tags = get_tags();
	$tags_count = count($tags);
	$number = ceil($tags_count / 2);

	$args = array('number' => $number);
	$tagsleft = get_tags($args);

	$args = array('number' => $number, 'offset' => $number);
	$tagsright = get_tags($args);

	$html = '<ul class="catleft">';
	foreach ($tagsleft as $tag){
		$tag_link = get_tag_link($tag->term_id);

		$html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
		$html .= "{$tag->name}</a></li>";
	}
	$html .= '</ul>';

	$html .= '<ul class="catright">';
	foreach ($tagsright as $tag){
		$tag_link = get_tag_link($tag->term_id);

		$html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
		$html .= "{$tag->name}</a></li>";
	}
	$html .= '</ul>';
	echo $html;
	?>

So what the hell is going on here? Here we go:


$tags = get_tags();
$tags_count = count($tags);
$number = ceil($tags_count / 2);

OK, so get_tags() is a standard piece of WordPress code. (Read about it here.) All this piece of code is doing is getting the tags for the blog (line 1). Then, it’s counting how many tags there are (line 2). Then it’s saying, divide that number by 2 and make it a nice, whole number (line 3). We have two columns, so that’s why we’re dividing by 2.


$args = array('number' => $number);
$tagsleft = get_tags($args);
$args = array('number' => $number, 'offset' => $number);
$tagsright = get_tags($args);

The get_tags() code can have a number of arguments. ‘Number’ is the number of tags you want to display. In line 1, we’re saying that number we determined (tags divided by 2) is how many tags we want to call in the ‘number’ argument. In line 2, we’re saying that get that number for the left column of tags.

The get_tags code also has an argument called ‘offset.’ It is just like it sounds – offset the list by a certain number and grab all the tags from that point. In the second half of the code, we’re saying “get that number of tags divided by 2, offset the list by that much and then grab that number of tags for the right side column.”


$html = '<ul class="catleft">';
	foreach ($tagsleft as $tag){
		$tag_link = get_tag_link($tag->term_id);

		$html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
		$html .= "{$tag->name}</a></li>";
	}
	$html .= '</ul>';

	$html .= '<ul class="catright">';
	foreach ($tagsright as $tag){
		$tag_link = get_tag_link($tag->term_id);

		$html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
		$html .= "{$tag->name}</a></li>";
	}
	$html .= '</ul>';
	echo $html;

The rest of this is just pretty standard output for tags (again, consult the Codex). What I did here was create two unordered lists, one for the right side and one for the left, and loop through all the tags. I’m displaying the name of the tag and linking to that tag’s page. You can CSS the hell out of this if you like.

What renders is a nice two-column structure that’s pretty even (unless you have super long tag names and that throws off your spacing a little bit). But you get the point.

But hey, smart guy! What if my number doesn’t neatly divide by 2!

That shouldn’t be a problem. Even if there’s one less tag in the right column, everything should still get called correctly. That’s because the ceil function rounds up. For instance, if you had 13 tags total, the ceil function would actually spit out the number 7 (it’s rounding up 6.5). So, you’d get 7 tags in the left column. The right column would still offset by 7 (which is correct) and would want to call 7 tags. But you’ve only got 6 left and it doesn’t care. It’ll display 6.

(Of course, if I’m talking crazy talk, feel free to tell me so.)

Want to see the tags columns in action? Go here: Paper and Cake blog (click on tags in the right column).

This entry was posted in Tips for You. Bookmark the permalink.

Leave a Reply