Image categories in WordPress

Finally, I just take few minutes to write this small tiny hack of WordPress. In 2 steps you get images associated with your posting.

I know it is a quick-and-dirty way of doing it. But at least it works !

Two things to do :

1. Add in “b2template.functions.php”, the following code :

function the_category_icon() {
$category = get_the_categoryIcon();
$category = apply_filters(‘the_category_icon’, $category);
echo convert_chars($category, ‘html’);
}

function get_the_categoryIcon() {
global $post, $tablecategories, $querycount, $cache_categicon, $use_cache, $wpdb;
$cat_ID = $post->post_category;
if ((empty($cache_categories[$cat_ID])) OR (!$use_cache)) {
$cat_name = $wpdb->get_var(“SELECT cat_name FROM $tablecategories WHERE cat_ID = ‘$cat_ID'”);
$cat_icon = $wpdb->get_var(“SELECT cat_icon FROM $tablecategories WHERE cat_ID = ‘$cat_ID'”);
++$querycount;
$cache_categicon[$cat_ID][‘name’] = $cat_name;
$cache_categicon[$cat_ID][‘icon’] = $cat_icon;

} else {
$cat_name = $cache_categories[$cat_ID][‘name’];
$cat_icon = $cache_categories[$cat_ID][‘icon’];
}
$cat_name = stripslashes($cat_name);
if (!empty($cat_icon)) {
$cat_icon = “img src='”.$cat_icon.”‘ alt='”.$cat_name.”‘>”; // dont forget to add < before img... ;o) } return($cat_icon); }

2. Alter the table structure of “wp_categories” by adding a field named “cat_icon” (type: tinytext).

Here below a sample of the content of that table :

INSERT INTO `wp_categories` VALUES (1, ‘News’, ‘http://idahocline.info/topics/news.gif’);
INSERT INTO `wp_categories` VALUES (2, ‘Photos’, ‘http://idahocline.info/topics/photography.gif’);
INSERT INTO `wp_categories` VALUES (3, ‘Travels’, ‘http://idahocline.info/topics/travel.gif’);
INSERT INTO `wp_categories` VALUES (4, ‘Internet’, ‘http://idahocline.info/topics/internet.gif’);
INSERT INTO `wp_categories` VALUES (5, ‘Unix’, ‘http://idahocline.info/topics/unix.gif’);
INSERT INTO `wp_categories` VALUES (6, ‘PHP’, ‘http://idahocline.info/topics/php.gif’);
INSERT INTO `wp_categories` VALUES (7, ‘Writing’, ‘http://idahocline.info/topics/writing.gif’);
INSERT INTO `wp_categories` VALUES (8, ‘Troubleshooting’, ‘http://idahocline.info/topics/bugs.gif’);
INSERT INTO `wp_categories` VALUES (9, ‘Web Development’, ‘http://idahocline.info/topics/programer.gif’);
INSERT INTO `wp_categories` VALUES (10, ‘Projects’, ‘http://idahocline.info/topics/creativity.gif’);