Fun with Wordpress categories
Jan19th
2009
••
The truth is, playing with what you can do with categories in Wordpress is fun. Being one that doesn’t like to use plugins for most functions, I enjoy tooling around in the code and trying to make the code do it for me.
So the question arose over on the Wordpress forums about how do make a list off all your categories with the post in each category. Pretty simple actually. Here’s the code i added to my themes functions.php file:
/* list categories with posts by tugbucket.net
*/
function cat_and_posts(){
foreach (get_categories(array('hide_empty'=>true)) as $category)
{
$catid = $category->cat_ID;
global $post;
$myposts = get_posts('numberposts=100&category='.$catid);
echo '<li>' . $category->cat_name . "\n";
echo '<ul>' . "\n";
foreach($myposts as $post) {
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
}
echo '</ul>' . "\n";
echo '</li>' . "\n";
}
};
/* end */
You call it your template by using <ul><?php cat_and_posts(); ?></ul>. Take a look at the cat_and_posts() example for a visual.
Not a lot of code involved. We set up a new function name and then query all categories that are not empty. Then we grab the category id of each and then start a loop printing out a list off all posts in the category.
If you have ever done anything with the category loop then this pretty straight forward.
*NOTE:
Notice my numberposts=100. zbrass noted in a comment that my script was cutting off at five posts. This is because the default for that attribute is five. I am using a pagination plug-in that stops posts per page at six. That’s why I used 100 for mine to override this. If you are not using pagination use 0 (zero) as the value to show all posts.
Grab the Description on display in a list
Another post came up the same day that asked about printing out the category descriptions in a like manner. This is based on the tag category_description.
Again this is pretty easy once you know how to loop through it and how to modify the tag.
/* descriptive cat list by tugbucket.net
*/
function desc_cats(){
foreach (get_categories(array('hide_empty'=>true)) as $category)
{
$catid = $category->cat_ID;
echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' .
$category->cat_name . ' (' . $category->count.$numposts.')</a>' . category_description($catid) . '</li>' . "\n";
}
};
/* end */
Here’s an example of the desc_cats() function. You can call it by placing <ul><?php desc_cats(); ?></ul> in your template.
Pretty simple stuff and with a little CSS added can be quite attractive. Each one would be pretty easy to incorporate with an accordian type script whether jQuery or MooTools for some intresting effects.













2009
••
How do I have it not insert a break tag if there is no description?
2009
••
John,
Instead of echoing out the list item, add it to a variable and then usu PHP strip_tags function to remove the break tag and then echo out the result like so:
$catdesc = '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' .$category->cat_name . ' (' . $category->count.$numposts.')</a>' . category_description($catid) . '</li>' . "\n";
$catty = strip_tags($catdesc, '<p><a><li>');
echo $catty;
2009
••
thanks for the very useful info. I have started using it on my web video site. I’m not finished customising it yet but you can see it here: Web Video Tips.
cheer
Tony
2009
••
Hi,
Thanks for this!
I have a question for now your code shows only 5 post within the cat.
if there are more posts in the cat how can i ad a link to these posts?
I don’t want to show more post but find out if there are more than ‘5′ posts and if so a link to the archive page.
foreach($myposts as $post) {
echo ‘‘ . get_the_title() . ‘‘ . “\n”;
}
—-HERE THE if more than 5 echo and than a link to the previous posts
echo ” . “\n”;
echo ” . “\n”;
I hope you can solve this , i looked everywhere for a solution.
Thanks!
2009
••
You could also use the Category Icons plugin in order to display icons assigned to categories : use get_cat_icon(”echo=false&cat=”.$catid); to display the icons in your foreach loop.
2009
••
@zbrass,
Thanks for bringing that to my attention. If you read my note above, I have modified the script a little. the ‘numberposts’ defaults at 5 so that’s why it was stopping. I edited it to solve that issue.
Now to your real question.
function cat_and_posts(){
foreach (get_categories(array('hide_empty'=>true)) as $category)
{
$catid = $category->cat_ID;
global $post;
$myposts = get_posts('numberposts=100&category='.$catid);
echo '<li>' . $category->cat_name . "\n";
echo '<ul>' . "\n";
$count = 0;
foreach($myposts as $post) {
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
$count++;
if ($count >= 5){
echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">View more</a></li>' . "\n";
break;
}
}
echo '</ul>' . "\n";
echo '</li>' . "\n";
}
};
We’re going to start a count of the posts. Once it reaches 5 it will add a link to the archives for that category and stop the script.
Now this will not make a link for categories with less than 5 posts.
Hope that works for you.
@submarine,
That looks like an interesting plug-in. I might play with it soon.
2009
••
Great Alan!
Thanks a lot.
But one thing to make it perfect would be that the link ‘view more’ would give an archive page that starts with post nr6
I do that now like this
echo ‘category_nicename . ‘/page/2/”>View more‘ . “\n”;
But if you have in your settings 10 posts on a page you have a problem there. You lose flexebility. cause page 2 would start with post 11.
You see what i mean?
Again thanks a lot!
2009
••
@zbrass,
I totally see what you mean. I think that would be best left for a manual setting like you are doing. My home page has a hack to only show 4 posts. My archive is using pagination set to 6 posts. So I would think it would be easier to set the category script to the max number you are showing and just send the link to page 2.
Since there is only a few areas that need modification, it’s not too difficult.
Glad it worked for you.
2010
••
Hey ! For the second code, the one with the descriptions, how can I do the following :
1) I want to add pagination. I have over 500 categories, and if all of them are listed WITH their descriptions, its a looooong and laggy page. So maybe only like..50 per page.
2) I want to add an in between the listed categories to space them out a little and make it look nice.
2010
••
ah for number 2 my html isnt shown. I said I want to add an “hr’ tag >< in between
2010
••
@Virendar
You should be able to modify the function like so.
function desc_cats(){
foreach (get_categories(array('hide_empty'=>true)) as $category)
{
$catid = $category->cat_ID;
echo '<div><h3><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' .
$category->cat_name . ' (' . $category->count.$numposts.')</a></h3>' . category_description($catid) . '<hr></div>' . "\n";
}
};
And call it like so:
<?php desc_cats(); ?>That will make each category in it’s own div with a <h3> and a link for the title and ending with a horizontal rule.
As for paginating the results, not sure off hand. That really has nothign to do with the base of this function.
2010
••
Thanks, that’s exactly what I was searching for
I did not get it thats its much easierer, if you use the functions.php ..
hope i’ll remember that
2010
••
Hello, i’m looking for help!
my website having problem with the display of posts under each categories.
Normally if you access any one of my categories (from sidebar or other place), you should see all posts listed.
But now it only show maybe 1 post or 2 post (1 full posts and 1 only the title being show).
i’ve try to deactivate all plugin to see if it works, but it still doesn’t work.
But when i switch it to another theme then it’s working fine.
i’ve another website can run the theme without the problem i mention…
Can you help me to settle this issue? Thank you……
2010
••
This is wonderful! After 4 days of research I found this code. Thank you so much. I have it working here: http://finditallroswell.com/wp/ - click on a ‘print coupon’ button and a window opens up listing the category descriptions. Please click on the ‘FROOTS’ print coupon. The list is showing correctly for the category descriptions, but it shows them all for ‘Restaurants’.
My question: is it possible to only show the description for “FROOTS” and not show the others?
Thanks so much!
2010
••
@Karen,
looking at your site and what I believe you are try to do, you really don’t need the whole function. Since you are passing a URL variable (cid) in your pop-up, you should be able to simply do:
<?php
$cat = $_GET['cid'];
$category = get_category($cat);
echo ‘<h3>’.$category->cat_name.’</h3>
<p>’.category_description($cat).’</p>’;
?>
That should grab your variable and then print out the title and description of the category the coupon is for.
2010
••
OK, that works but gives me the wrong category description:
http://finditallroswell.com/wp/wp-content/themes/couponpress/_print.php?cid=110
It’s giving another for ‘Gallery Spa’ (which is the category after Froots) instead of the current ‘Froots’.
Here’s what I used:
2010
••
2010
••
@Karen,
can you email me your “_print.php” to alan[at]tugbucket.net I’ll take a look and see if I find anything.