The list of categories is displayed in the sidebar or on any part of the theme will be in a single column. But for people who have many categories, It will be better to Split into columns so that it may not take much. Here is the by which you can split your categories’ list evenly into two or more columns which can save space and not make your pages longer. Usually in WordPress, the following code is used to call the categories’ list (you can find it in one of the sidebar files like Sidebar.php)
<?php wp_list_categories(); ?>
Now, we will try and split the categories into two columns. Replace the above code with the following
<?php
$cats = explode("<br />",wp_list_categories(‘title_li=&echo=0&depth=1&style=none’));
$cat_n = count($cats) – 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>’.$cats[$i].'</li>’;
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>’.$cats[$i].'</li>’;
endif;
endfor;
?>
<ul class="left">
<?php echo $cat_left;?>
</ul>
<ul class="right">
<?php echo $cat_right;?>
</ul>
Basically the above code will split your categories and put them into two lists (left and right). Now all you have to do is style them so that they float left of each other. Add this code to your style.css file
.right {float:left; width:140px;}
.left {float:left; width:140px;}
This will make the categories displayed evenly in 2 columns like this.
You can make any other adjustments you want according to your theme. Try it and do mention any other methods to improve the splitting of categories list.
Hola I appreciate the new entry.
Or even shorter: $cat_n = (count($cats)) – 1;
Okay, something is wrong with the code, but I figured it out.
This:
$cat_n = count($cats) – 1;
Needs to be replaced with this:
$cat_n = count($cats);
$cat_n = $cat_n – 1;
No idea why, but now it works!
Hannah,
Thanks a lot for the tip
Its not working for me 🙁
You have to check your CSS file and should make some necessary changes to make it work.
I get the same error as onelargeprawn Parse error: syntax error, unexpected ‘=’
You did say that “in some themes the direct code will not work. You need to tweak it a bit”
Could you elaborate on that please for us newbies?
I am using Studiopress Lifestlye Theme
Thanks
Hey, its not working ! 🙁
In some themes the direct code will not work. You need to tweak a bit.
I tried to insert the code into the sidebar, and got this error: Parse error: syntax error, unexpected ‘=’
The “=” sign the error mentions is on the line reading “$cats = explode(“”,wp_list_categories(’title_li=&echo=0&depth=1&style=none’));”
How would I go about getting this to work?
Thanks.
This is really unbelievably helpful, thank you! The best solution I’ve found so far, exactly what I was looking for!
Is there anyway to exclude certain posts from these columns?
That’s a rare post… good one.. will look into that in need….