Host Unlimited Web Sites for Only $10 per Month!
Powered by MaxBlogPress  

 

Very Easy Wordpress Theme Hack: Show Category Images | JTPRATT's Blogging Mistakes
JTPratt's Blogging Mistakes





Home » Very Easy Wordpress Theme Hack: Show Category Images


 
 
 

Posted in:

blog-setup category image blogging-mistakes category image wordpress category image
13,696 views


I’m going to show you how to add some simple PHP code to your Wordpress template and make linked images appear instead of text for your categories in posts. All you need to know is simple PHP, some basic HTML, and how to make your own graphics.

**UPDATE** May 2008
As the last comment at the end of this page shows there is now a Category Icons Wordpress Plugin you can download to install to manage your category icons right in your dashboard. If you prefer to hack your theme, the code below will work just fine! The cool thing about the plugin is that they give you ways to do one or multiple category icons, but also a methods for putting icons with your categories in the side bar as well! I’m always very grateful when a good plugin come along! Now back to the original post…

Have you ever wanted to hack Wordpress and just make it do something you always wanted it to do? I think of things I want it to do all the time but don’t always have the hours to devote to finding the answer. I’ve searched for PHP code snippets for Wordpress and tutorials and how-to articles as well and haven’t seen much that really helped me. Until today - when I read the tutorial page at WPDesigner today. I didn’t get exactly the code I needed, but pretty close. I just needed to hack it a bit to get it to do what I wanted.

First, this Wordpress tutorial hack assumes a few things.

  1. You’ve modified your Wordpress template code before
  2. You can modify simple PHP
  3. You can write simple HTML
  4. You can make your own graphics

First things first - here’s the code:

<h3><b>Posted in:</b></h3>
<?php foreach((get_the_category()) as $cat)
	{
	$catname =$cat->category_nicename;
	echo "<a href\"/category/";
	echo $catname;
	echo "/\">";
	echo "<img src\"/wp-content/cat-icons/";
	echo $catname;
	echo ".png\" alt=\"$catname category image\" border=\"0\" /></a>\n";
	}
?><br />

I hacked this code from this tutorial page on WPdesigner. The code they had was only good to display one category per post. I hacked it because my blogs all have posts assigned to multiple categories. Also, I wanted the images to all be linked so people can just click them and go right to that category page. Let’s look at the pieces of this code you might need to modify.

In line 1, you might want change “Posted in:” to alternate text like “Filed under:”, etc.

In line 5, if your permalink structure is different than the default, you may need to change /category/ to what you are using.

In line 8 change /wp-content/cat-icons/ to wherever you will be storing your category icons in your site.

In line 10, if you are using gif’s or jpg’s change .png to your graphic file format.

Now all you have to do is create one image for every category you have an place them in the folder you just created on the server. Make sure that the graphics are named exactly as your categories, and if your categories have multiple words just add a hyphen in between each word. That’s it! All you have to decide now is where you want these category icons to appear, before or after your post - and add them into the appropriate place. I placed mine just after the title of the post on my single post pages - as you can see at the top of the post on this page. You can hack this category icon code a hundred other ways to do what you want. I think in the future I’m going to use it to replace the text category links in my sidebar. The only caveat to this (and the code hack above) would be the fact that if you don’t have an image a text link will display (in FF, or broken image in IE). So - if you create new categories in the future, make sure you create a new image for each.

My Blogging Mistake for this post is that I didn’t decide to customize and hack my Wordpress Theme before now to create my own layout. Do you have a question or a new way to hack up this wordpress category icon PHP code? Comments and post it now!


28 Responses to “Very Easy Wordpress Theme Hack: Show Category Images”

  1. The Smorgasbord Has the following to say...

    This was very easy to do - thanks for the code and the tip!

    The Smorgasbord’s last blog post..Convert Vinyl Records to MP3

  2. 50 tips y trucos para optimizar Blogger y WP | Blog en Serio Has the following to say...

    [...] Imágenes para las categorías [...]

  3. Blogbuster: блоги, blogs, blogging, RSS, Wordpress, социальное Has the following to say...

    25 неотстойных хаков для WordPress

    Динамическое слайдшоу в шапке — позволяет крутить картинки в шапке вашего блока, придает динамику; демоверсия.
    Пузырь комментариев — гр…

  4. 25 hacks para dinamizar seu Wordpress! — Bytes a Go-go! Has the following to say...

    [...] Imagens para categorias. Adiciona imagens para cada categoria do seu blog e são mostradas no post se você preferir. [...]

  5. Blog Verde » 25 Hacks pra dinamizar o Wordpress Has the following to say...

    [...] Imagens para categorias. Adiciona imagens para cada categoria do seu blog e são mostradas no post se você preferir. [...]

  6. 63 Essential Wordpress Hacks, Tutorials, Help Files and Cheats | Speckyboy - Wordpress and Design Has the following to say...

    [...] Show Category Images (Source: [...]

  7. 63 essenziali Wordpress Hacks, Tutorials, Help Files e Cheats : technorati.it Has the following to say...

    [...] Show Category Images (Source: [...]

  8. Wordpress Tutorials/Hacks « The house of software freeware Has the following to say...

    [...] Show Category Images (Source: [...]

  9. Mastering Your WordPress Theme Hacks and Techniques Has the following to say...

    [...] 10)Show Category Images- How to add some simple PHP code to your WordPress template and make linked images appear instead of text for your categories in posts. [...]

  10. letrodectus Has the following to say...

    Great hack! thanks =

  11. 63 Essential Wordpress Hacks, Tutorials, Help Files and Cheats | Speckyboy - Wordpress and Design » 쟈스민(JASMIN) Has the following to say...

    [...] Show Category Images (Source: [...]

  12. paidnetpedia.com Has the following to say...

    [...] sticky”- When we need to have certain articles ’stay at the top’ longer than others. 10)Show Category Images- How to add some simple PHP code to your WordPress template and make linked images appear instead [...]

  13. Submarine Has the following to say...

    Hi

    here is a plugin that do the job :

    Category Icons Plugin.

  14. Remkus Has the following to say...

    That is a very nice Hack! I realize there is a plugin now, but I think I am going to just edit this in one of my projects. Thanks for writing it down!

  15. What is Category Icons ? « Category Icons Has the following to say...

    [...] : JTPRATT’s Blogging Mistakes ( here & here ), Performancing Helping Bloggers [...]

  16. 155 Wordpress Resources, Tutorials, Plugins, Themes, AJAX, Podcasting…WP Monster List | Speckyboy - Wordpress and Design Has the following to say...

    [...] 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress Comments and Trackbacks. 89. Customize your WordPress Login. 90. Date Image [...]

  17. 155个 WordPress 资源、指导、插件、主题、AJAX、播客…… —— WP 详细清单(译) | æ™´å¤©é‡Œçš„ä¸€é“å… Has the following to say...

    [...] 84. No More CSS Hacks 85. Create a Dynamic Sticky 86. Styling Individual Posts Using the_ID 87. Show Category Images 88. Separate WordPress Comments and Trackbacks 89. Customize your WordPress Login 90. Date Image [...]

  18. 155个 WordPress 资源、指导、插件、主题、AJAX、播客…… —— WP 详细清单 | Awesomer Has the following to say...

    [...] Hacks84. No More CSS Hacks85. Create a Dynamic Sticky86. Styling Individual Posts Using the_ID87. Show Category Images88. Separate WordPress Comments and Trackbacks89. Customize your WordPress Login90. Date Image [...]

  19. 40+ Most Wanted Wordpress Tricks and Hacks Has the following to say...

    [...] Create and show categories image [...]

  20. Erika Has the following to say...

    I’m always a fan of wordpress hacks! I’m weary of plugins because of compatibility, but (almost) nothing can be incompatible with good ol’ fashioned semantic code. :)

    Erikas last blog post..Miami Web Design

  21. Tom Sawyer Has the following to say...

    This is some good information even if I’m getting it late. This is one of reasons our page is one of my tabs when I open Firefox.

    Tom Sawyers last blog post..Inspirational Professor Dies; Inspiration Continues

  22. Submarine Has the following to say...

    Hi !

    Here is the new address of Category Icons website :
    http://www.category-icons.com

    Submarines last blog post..What’s new in 2.0.6 ?

  23. wisdom Has the following to say...

    I have not heard about this plugin before, I will have to look into it.

  24. Becky of Love Astrology Has the following to say...

    Thanks for the tutorial. I’m going to give this a go, wish me luck!

    Becks x.

    Becky of Love Astrologys last blog post..The Gemini Horse

  25. Tom Has the following to say...

    Any idea how to make the category based images appear right where the date is.

    My website isn’t chronological and really doesn’t need dates for anything. I figured that having the category image right there would be a neat idea.

    Thanks in advance!

  26. TechnoSamrat Has the following to say...

    I’m using the Category Icons Wordpress Plugin and it is very simple….

    TechnoSamrats last blog post..WebVideoCap - Download Videos From Google Video, YouTube, Yahoo Video, iFilm and Metacafe

  27. admin Has the following to say...

    @Tom - sure, all you have to do is hack your theme in Design mode, adding the code right where you want images to appear (and on each theme page you want to use them on)

  28. Blogging for Profit Has the following to say...

    OMG!
    I’ve missed so much useful tricks and tips how to hack my WP installation. Now, I want to collect this info for me to do as what you’ve done. I’ll be around, man

    Blogging for Profits last blog post..Decorating Home Office With Rugs

Question or Comment?? Spill it Now...

Jumping for Joy over comments!

We Reward Comments!


We dofollow links, and get your latest blog post as a byline under every new comment from the "CommentLuv" plugin! Top commenters for every month are listed on every page of this site in a sidebar widget linked back to your URL! We would like to reward you for becoming part of our community! Your comment is valuable not only to us, but also all the other readers of this blog!
 

 


Click to add smilies to your post! = =[] ^=( =(( =(| =)r =|8 =0 =)~ =00 =( =;; =)] =;;;