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…

11 thoughts on “How to assign a specific icon for a post

  1. Hi!
    Thanks for the info, very cool stuff! I’ve just tried it and it worked totally fine. Is there maybe a way to use the ID or name of a category instead of the direct link to the icon file?

    Best wishes,
    Andy (not from Weeds and no, i don’t use bananas ;) )

  2. Yes, but I thought you would not use an assigned icon… If you want to use a category ID, just replace :
    $content = '...';
    by :
    $content = get_cat_icon("echo=false&cat=".$values[0]);

  3. for posts with multiple categories, i only want to show the icon of one of the categories.. doesnt matter which one.. how do i do that?

  4. Thanks for your response! Actually with the edited code the page now doesn’t load anymore. I’ve tried some different approaches but no chance to make the page load. I get a white page. Any idea?

  5. So I managed to get the icons working for categories, and I read your post on specific icons for post but can’t seem to get the functions.php right. I always end up with a parse error in line 4. Can you explain for a noob?

  6. Hi!
    Any chance to get an update on that issue with the code for the unique ID assignment? Still, everything is broken when I add your code to the functions.php

  7. Cool trick buddy :) I was searching this on how to add icons for specific post or category to make by blog unique and your article solved my problem :)

  8. I agree with Why Guess? This is a great plugin but way too hard to implement. I donated $10 and hoped for assistance with the emails I have sent but I guess not.