How toHow to assign a specific icon for a post

Someone (onkelandy from Weeds ? :) ) asked me if it was possible to assign a specific icon for just a post :

Normally icons are shown depending on the category a post is put in. If a post is related to several categories the priority system comes into place. Anyhow there are some cases where that icon just doesn’t fit to a specific post. So it would be very cool if it was possible to define a specific icon/icon category for just that post by using the “custom field” that appears on the “create post” page in the admin area.

Yes, it is possible, thank to the filter ‘category_icons‘, as I’ve written about in this post. Just make sure you add a custom field named “caticons” and the URL of the icon/image as the value. Paste the following code into functions.php of your theme :

function bm_unic_icon ($content) {
  $values = get_post_custom_values("caticons"); // Get the values of the field 'caticons'
  if (isset($values[0])) {
    $content = '<img src="'.$values[0].'" alt="my specific icon" />'; // Get the icon URL
  }
&nbsp; return $content;
}
add_filter('category_icons', 'bm_unic_icon',15); // Use the filter 'category_icons'

Of course, it’s up to you to customize the 4th line, in order to make a W3C compliant output, for example…

How toHow to modify the category icons through functions.php

I’ve created a filter named category-icons in the plugin. I used that filter to add SEO Friendly Images compatibility.

So, if you want to play with it in order to modify the icons (rollover or other thing), you can do that in functions.php of your theme. You’ll perhaps need some knowledge about parsing stuff, but it’s worth it. And if you write some cool functions, why not share them with us ? :-D

Example (a very simple one) : Continue reading