The category_description() function is not a Loop function. It works on a category archive page or if given an explicit category ID. But in just some random Loop it does nothing in particular.
You’ll need to use get_the_category() to get the category array and then loop through that to get the description(s) of the category(ies) that the post is in.
Thanks Otto … I don’t suppose there’s a snippet of code somewhere that does what you’re suggesting?
Example code:
$cats = get_the_category();
foreach ($cats as $cat) {
echo $cat->description;
}
OK, I’m missing something obvious here … here’s my code, which is returning none of the category descriptions:
<ul>
<?php $myquery = $wp_query; ?>
<?php query_posts('cat=6,8,10,12,14&showposts=5');
while (have_posts()) : the_post() ?>
<li>
<?php $cats = get_the_category();
foreach ($cats as $cat) {echo $cat->description; } ?>: <a href="<?php the_permalink() ?>" title="<?php _e(''); ?> <?php the_title(); ?>"><?php the_title() ?></a></li>
<?php endwhile; ?>
</ul>
Any thoughts?
That should work. You must either not have the posts in the categories, or the categories have no descriptions.
Alternatively, you can also try this in the foreach loop:
echo category_description($cat->term_id);
Nope, tried that too, still nothing. Double- and triple-checked: the categories definitely have descriptions, and there’s a test post in each one. The colon and the post title are appearing – just the category description is not.
Grrrr …
Just occurred to me: Are you running the latest version of WordPress (2.3.3)? If not, upgrade. Categories changed in 2.3 and previous versions won’t work with the code I’ve given you.
Also, previous versions of WordPress have security flaws. Upgrading is really required.
We’re running 2.2.2 for a very specific reason: we had to hack the get_calendar component (via a plugin, not modifying core code) so that it would only display posts from a single category. Couldn’t figure out any other way to do it … and as I understand it, there were fundamental changes in the way 2.3 handles categories that would make the calendar inoperable.
Well, I would suggest that you figure out a way. Because your site is vulnerable by you running older buggy code.
While categories did change, you can modify your plugin to deal with those changes. This is better than simply letting your site remain open to being hacked.