Categories
Package/plugin/module WordPress

Plugin: Category Image(s)

This plugin has been updated! Comments to this post are now closed. For the latest download, documentation, and link for posting new comments related to this plugin, visit the plugin’s new homepage at:
coffee2code.com/wp-plugins/category-images
Name:
Category Image(s)
Author:
Scott Reilly
Version:
1.2
Last updated:
17 May 2005
Description:
Displays image for each category associated with a post. The number of images displayed can be limited, and the image file extension type and file location can specified.

Notes:
  • c2c_the_category_image() basically extends the functionality of WordPress’s core function the_category() to add the ability to display a category image instead of the category name in the link to the category archive. The function with its $image_extension argument defaulted to ” could be a drop-in replacement for the_category().
  • As is done the_category(), the image or text for each category is also a link back to that category’s archive.
  • If $image_extensions is defined, but no image is found for the category, then nothing is displayed for that category.
  • The images must be given a nice-name version of the category name. This means all non-alphanumeric characters must be stripped from the name, all letters made lowercase, and all spaces replaced with ‘-‘. For example:
    Category name ==> Nice-name to use for filename :
    WordPress ==> wordpress
    Scott’s Plugins ==> scotts-plugins
  • The number of category links/images displayed can be limited via the $limit argument
  • Category a image <img> tags are defined with class="catimage" for stylesheet manipulation
  • No global site-specific settings need to be configured.
  • Must be run inside “the loop.”
Installation:
  1. Download the file category-images.zip and unzip it into your wp-content/plugins/ directory.
    -OR-
    Copy and paste the the code ( category-images.phps ) into a file called category-images.php, and put that file into your wp-content/plugins/ directory.
  2. Activate the plugin from your WordPress admin ‘Plugins’ page.
  3. Create category image (.png unless you specify the extension in your call to c2c_the_category_image()) for each category you wish to have an image for, placing the image into the wp-content/images/ directory (unless you specify another location in your call to c2c_the_category_image()). Remember to give the filename the nice-name version of the category name (see note above).
  4. Modify your index.php at a point within “the loop” to make use of the function (assuming, of course, you’ve already defined categories and assigned posts to them). The simplest use would be to replace the index.php call to the_category() with c2c_the_category_image()
Functions:

function c2c_get_category_image($category, $image_extensions='png gif jpg' $image_dir='/wp-content/images/')

This is a helper function that retrieves the URL of the image for a specified category. Returns ” if no image was found:

  • $category : array for a single category
  • $image_extensions : (optional) a space-separated list of image extensions to consider for use for the image(s) to be displayed, in the order they are to be considered; if defined as ” then no image is retrieved and the category name is displayed instead (a la the_category()); default is ‘png gif jpg’
  • $image_dir : (optional) the directory in which to look for the image(s); default is ‘/wp-content/images/”

function c2c_the_category_image($seperator='', $parents='', $image_extensions='png gif jpg', $image_dir='/wp-content/images/', $use_name_if_no_image=false, $start_from='begin', $limit=999)

This function does the actual display of the post’s category image(s):

  • $separator : (optional) text to insert between each image to be displayed (examples: ‘, ‘; ‘<br />’); if defined as ” (which it is by default), then like the_category(), it will display the image(s) in an unordered list.
  • $parent : (optional) honestly, I’m not quite sure if/when this argument is ever used or what it’s meant for, but I’ve taken it into account; default is ”
  • $image_extensions : (optional) a space-separated list of image extensions to consider for use for the image(s) to be displayed, in the order they are to be considered; if defined as ” then no image is retrieved and the category name is displayed instead (a la the_category()); default is ‘png gif jpg’
  • $image_dir : (optional) the directory in which to look for the image(s); default is ‘/wp-content/images/”
  • $use_name_if_no_image: (optional) if no image for a category is found, should the name be shown instead?; default is ‘false
  • $start_from : (optional) display images starting from the ‘begin’ or the ‘end’; default is ‘begin’
  • $limit : (optional) the maximum number of images to display for a post; default is ‘999’
Tips & Examples:
  • You can opt to treat only certain categories as having a graphical representation by defining an image for them but not creating images for the non-graphically represented categories (as in you can have a subset of categories that dictate what image to be displayed for the post)
  • You can take advantage of the $image_extension and/or $image_dir arguments to display different category icons under different contexts, i.e. if showing a $single post, choose to use a ‘png’ image, otherwise use ‘gif’; or locate your images in different directories ‘/wp-content/images/cat-images/small/’ and ‘/wp-content/images/cat-images/large/’ and decide on context where to get the image(s) from:

    // In showing the post singularly, then use a larger image, else use a small image <?php if ($single) { c2c_the_category_image('', '', 'gif', '/wp-content/images/cat-images/large/'); } else { c2c_the_category_image('', '', 'gif', '/wp-content/images/cat-images/small/'); } ?>


    // Show all images in a comma-separated line <?php c2c_the_category_image(', '); ?>

Related Links:
  • WordPress Support Forums announcement of Category Image(s)
Release Log:
  • 17 May 2005: v1.2 released — Compatibility fix for WP1.5.1
  • 23 Mar 2005: v1.1 released — Same as v1.0 but compatible with WP1.5+
    • Change to accommodate new get_category_link() arguments
  • 23 Mar 2005: v1.0 released — Last version compatible with WP1.2. Changes include:
    • Added argument $use_name_if_no_image=false to allow showing cat name if no image was located
    • Tweaks to update c2c_the_category_image() with changes made to the_category() (mostly rel= stuff)
    • Changed default category images directory from /wp-images/ to /wp-content/images/
  • 12 Oct 2004 : v0.92 released — Fixed a pair of typos that prevented images from showing
  • 23 Sep 2004 : v0.91 released —
    • Now supports listing multiple image extensions
    • Prepended all functions with “c2c_” to avoid potential function name collision with other plugins or future core functions… NOTE: If you are upgrading from an earlier version of the plugin, you’ll need to change your calls from the_category_image() to c2c_category_image()
    • changed from BSD-new license to MIT license
  • 27 Jun 2004 : v0.9 released to the public
Copyright & Disclaimer:

Copyright (c) 2004-2005 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements:
This plugin is essentially an extension of WordPress’s core the_cagetories() function, so the core developers are responsible for a majority of the code present in the function.

149 replies on “Plugin: Category Image(s)”

Great plugin, very easy to implement. One question. Is it possible to display the category image as a background to the post, allowing the text to flow over the image??

I think that WordPress 1.5 changed underneath you, and left this version of the plugin (0.92) slightly broken.

The category ID generated in the links is always zero.

I think this is because the get_category_link() function changed; all the old code I’ve seen uses three parameters – a zero, the category ID, and the “nice name”.

The new version from WP 1.5 appears to only use the category ID.

Thus, all the links wind up as cat=0.

It’s an easy tweak, I think. After having made it, the category IDs on my page work properly in WordPress 1.5.

(Mind you, I’ve only been using Word Press since 1.5, and only for about a week, so I may be wrong… but the code itself was pretty clear.)

Lou: Thanks for figuring out the problem! Turns out get_category_link() was changed the day before WP 1.5 was released, but by then I had already tested the plugin against 1.5 and it had worked.

khaled: Try the latest from the dev.wp-plugins.org repository and let me know if that works for you.

Glad to have helped, Scott. It’s a neat plugin.

Darn those moving targets, anyway! =)

I do think that the code is a little messed up when it comes to using the “parent” option. It works fine if it’s blank or caught by the “default” value, but if you try and use “single” or “multiple” then the output is fairly strange.

Have you had a chance to look at this, or would you like me to have a longer (more than reading through the code) look at it?

At the moment, I’m using ” on my site, and then also using the text-based category function to get the other display, so it isn’t a problem for me, but it might be for others.

I’d be happy to have a look at it.

Joe: Possible? Yes. In an easy and direct manner? Probably not. The function can assist you in obtaining the category image file location (by calling c2c_get_category_image() with the appropriate category ID and other arguments). However, you would need to introduce a bit of CSS to make the image the background image for just that post, which is a bit beyond the scope of this plugin to do. Here’s a top-of-my-head solution that hasn’t been tested:

In index.php, you should have a line like this:
<div class="post">

Change it to something like:

<?php
$cat = get_the_category();
if (empty($cat[0]))
   $background_style = '';
else
   $background_style = " style=\"background:url('" . c2c_get_category_image($cat[0]) . "');\"";
?>
<div class="post"<?php echo $background_style; ?>>

This approach makes some assumptions (i.e. that only the first category retrieved is the one that is supposed to have an image) and very likely doesn’t work exactly as I wrote it out, but it should be close. I think.

Lou: If you have a chance to look it over, please, by all means feel free! I’m swamped with stuff at the moment. To tell you the truth, I haven’t fiddled with the “parent” option much. I used WP’s the_category() as the basis for this function and tried to minimize the number of changes I did in introducing the ability to handle images.

I’m trying like crazy to get this thing work on my site. I can get the default category to show up but none of the others. I have updated to the 1.1 version of the plugin with no luck.

Please help me. My wife really want’s this on her site! 😉

Didn’t mess with it much but it keeps giving me T_Variable errors. I don’t know much php but I’ll learn what I need to. Thanks a million for the tip, I appreciate it. Awesome plugins man. How would one learn to write plugins? Learn the API? Just curious.

hi there. great plugin. i am in the process of switching my blog to wp 1.5 and decided to use only plug-ins from now on. i used to handle my category icons via my-hacks. now i have two questions:

1. is there any way to call 2 tags from the title=”” tag? i used to be able to call $category->cat_name and $category->category_description. now this does not seem to work somehow. how can i do that?

2. i noticed that there is quite a big white space between icons if there are several categories to a post. is there any way to reduce that space? it looks like two spaces now, i would like to have them line-up without space inbetween.

cheerz…

I’m going to hate myself for asking this I’m sure, but I’m not getting the output I expect. I see the images, and there is a link, BUT all the links, no matter what image (category) are to www.clarkburbidge.com/index.php/archives/category/. The pertinant part linking you to that actual category is missing. Please break it to me softly. I have a feeling it’s right under my nose.

I would also like to have the code post a top image and a bottom image for rollover purposes. Anyone got any ideas of how I’d tweak the plugin to do so..?

Plugin updated to v1.1

Changes:

* Added argument $use_name_if_no_image=false to allow showing cat name if no image was located
* Tweaks to update c2c_the_category_image() with changes made to the_category() (mostly rel= stuff)
* Changed default category images directory from /wp-images/ to /wp-content/images/
* Change to accommodate new get_category_link() arguments (fix necessary for the plugin to run in WP1.5)

v1.0, the last version of this plugin compatible with WP1.2 (basically the same as v1.1 except without the necessary changes to make it WP 1.5 compatible), is available via dev.wp-plugins.org at dev.wp-plugins.org/browser/category-images/tags/v1.0/

NOTE: v1.1 and beyond are no longer compatible with WP 1.2.

Scott-
I’ve been trying to get category images in the sidebar, but it’s been very difficult. I read your post from September that you’re looking to make this work with wp_list_cats, but I guess that hasn’t happened yet.

Is there any way, ugly as it may be, to hack this functionality?

Hey,
I was wondering if you have any plans of making this compatible with WP 1.5 or higher. I’d really like to be able to use this plugin. If you know of any other Category Image plugins also, please let me know.
Thanks.
-Brendan

Plugin updated to v1.2

The plugin has been updated to include a fix making it compatible with WordPress 1.5.1.

As for the sidebar requests, that will have to wait until a subsequent release.

jors: What you want to look for in index.php is this line:

<?php the_category(', ') ?>

That’s what you want to replace with something like:

<?php c2c_the_category_image(', ') ?>

Ok Scott, thanks for your time. But I’ve only found the the_category() function on wp.php file since WP 1.5 (1.5 & 1.5.1) and change it there. Anyway, I activated the plugin, and WP is throwing to me the following errors:

| |
Warning: Cannot modify header information – headers already sent by (output started at /home/enchufad/domains/enchufado.com/public_html/wp-content/plugins/category-images.php:1) in /home/enchufad/domains/enchufado.com/public_html/wp-admin/admin.php on line 10

Warning: Cannot modify header information – headers already sent by (output started at /home/enchufad/domains/enchufado.com/public_html/wp-content/plugins/category-images.php:1) in /home/enchufad/domains/enchufado.com/public_html/wp-admin/admin.php on line 11

Warning: Cannot modify header information – headers already sent by (output started at /home/enchufad/domains/enchufado.com/public_html/wp-content/plugins/category-images.php:1) in /home/enchufad/domains/enchufado.com/public_html/wp-admin/admin.php on line 12

Warning: Cannot modify header information – headers already sent by (output started at /home/enchufad/domains/enchufado.com/public_html/wp-content/plugins/category-images.php:1) in /home/enchufad/domains/enchufado.com/public_html/wp-admin/admin.php on line 13

Any known issue with this? Thanks.

I’m getting an error that is confusing the hell out of me.

Parse error: parse error, unexpected T_STRING, expecting ‘&’ or T_VARIABLE or T_CONST in /home/virtual/site121/fst/var/www/html/rantfarm.net/blog/wp-admin/admin-functions.php on line 56

I was able to have the category text and the image appear at the same time but for some reason, i removed that code and now i’m stuck. I want to have both category title and image appear at the same time. can someone please help?

by the way, that error only happens when i click the Edit Post link next to any post. I removed the category image code from my index.php because i didn’t want my whole blog to be down because of this.

I have copy/pasted your code exactly as it appears in the file to my server but it isn’t working. it gives me this error message:

Filed under:
Fatal error: Call to undefined function: c2c_the_category_image() in /home/virtual/site121/fst/var/www/html/rantfarm.net/blog/wp-content/themes/benevolence/index.php on line 13

I’m not a php coder, so I don’t know if I’m asking for something difficult, but would it be possible to set this to allow for different images in different themes? I think all you’d have to do would be change the default to look for a catimages directory in the active theme directory, but I suppose I’m not really certain that’s the case.

Ok I installed wordpress for a friend and he asked me for the icon category plugin.. I installed it and it works but I would like to change some things… you can see the result at www.telefilm-central.org/tru/

1. I don’t want that blu border around the image… it really looks bad…
2. I would love the image on the right of the text with the text all around it not above it…
What should I do?

Well, I followed the instructions, named all the files correctly, and the only place I could find the_category() was in wp.php, so I changed it there. Unfortunately, the category images are not being shown still in 1.5.1.

I can’t think of anything. Anyone have any ideas?

Hmm, I must be a total newb, because I’m not seeing which index.php I’m supposed to change. I’m assuming it’s the index file in the currently used theme. But the one I’m using (Connections) doesn’t use the get_category function. The Default theme does, so I switched to that theme, edited the index.php, activated the plugin. Nothing.

My server has WP 1.5.1.2.

I’m trying to get this to work, but nothing is happening. Can one of you techie people please post a simple example in plain english and maybe provide some sample files. All of this back and forth with broken code is causing my eyes to cross…

Excellent plugin – thanks!

I thought I’d give a very small correction to some CSS advice above. On my site, hacktopia.org, I wanted a slashdot-like appearance… I wanted the text flowing around the image, which would be in the upper right. I’m very new to CSS, but after playing around for a bit, here’s what worked:



Cool, got it working now! Very nice. Would be nice to get the parent option working. I’ve got one sub-category that I would like to have inherit the category image from its parent.

Pretty cool stuff.

I love this plugin, but I cant seem to get it working. Im using WP 1.5.1.3 and I’ve done all the instructions above…made sure my file names are “nice”, etc etc and its still not working. No image shows up at all…..
Help!

Hello! Excellent plugin, Scott. I’ve added a function to your plugin on my site that allows the category list on the siderbar to display the category pictures instead of the category names. To do this to your own, copy the list_cats() function from template-functions-category.php and paste it into the category-images.php file, renaming it list_cats_with_images(). Call that function in sidebar.php with the same arguments as list_cats() to test if you’ve overridden it correctly. Now, replace these lines in your new list_cats_with_images() function:

$link .= ‘>’;
$link .= apply_filters(‘list_cats’, $category->cat_name, $category).’‘;

With these lines:

$link .= ‘>’;
$link .= ‘‘;

That will make images appear instead of the category names! I have made my category images with the text of the category in the picture, so it’s still obvious what category it is. See the sidebar at my website at sandstone.brewsters.net for an example of this.

Note that we removed the apply_filters function – I don’t have any idea what this does. Also, I haven’t tested the function with any other arguments other than the WP 1.5.1.3 default, so please post here if you find that this change/addition breaks other argument sets. Thanks again, Scott!

Uh that code was supposed to be:

Replace these lines in your new list_cats_with_images() function:

$link .= '&gt;';
$link .= apply_filters('list_cats', $category-&gt;cat_name, $category).'';

With these lines:

$link .= '&gt;';
$link .= '</a>';

Uhh… I realized that in my post (#89), the css got stripped out of the comment. But if you want to see an example of slashdot-like alignment, look at hacktopia.org and look at the source of the page. I’ve actually just changed it to show the images on the left. I think that looks better.

Hi, I just can’t seem to ge htis working… I am noew to all of this (PHP, CMS, etc). but not to CSS & web dev. SO, any hints on what I may be doing wrong?

I have created a directory for wp-content/images/ and placed 2 test images there (both nice-named versions of categories with posts). I enabled the plug-in; I enabled .gif as a selector.

I refresh & all that happens is that the existing category/link dissppears. No graphics. Any hints for a new user?

Comments are closed.