views:

1680

answers:

3

I am building my first wordpress theme and was hoping that when the user made a new category, there was a way of automatically generating a thumbnail for it from the first image uploaded as a post to this category (it is a portfolio theme).

Does anyone know how this might be coded?

It cannot rely on hard coding as the user does not know how to do this and I cannot do it for every category they make.

Any pointers for code or even plug-ins would be grerat.

+1  A: 

It looks like this plugin or similar might do what you are looking for.

Tom Haigh
Any ideas of a particular one ?
DanC
was the linked one no good?
Tom Haigh
sorry, completely missed the link, I'll investigate!
DanC
+1  A: 

Hook onto an appropriate action such as publish_post so that, when a new post is created and sent out to the world, you scour the HTML for image tags. If the post's category does not already have a thumbnail associated, use an image from this post to create a thumbnail and associate it with the category. If you only want this to occur for new categories and not existing categories, track which categories were already present when the plugin was installed.

You could use WordPress' existing media infrastructure to store the thumbnail and the wp_postmeta table to track your category-thumbnail associations. PHP comes with a library for image manipulation, and HTML scouring would be done with string manipulation.

Hao Lian
+2  A: 

It doesn't auto-create an icon based on the first post, but this is a "category icon" plugin which would enable your users to specify an icon for each category through a page in the control panel. It isn't fully automated in that users can't upload images directly through its admin page, but they could upload to the media library and then reference the URL.

Note that on the WP plugin page this plugin is listed as compatible up to 2.6.1 but if you search the compatibility list for 2.7 it's on there as working, and indeed I have it working on my 2.7.1 site.

http://wordpress.org/extend/plugins/category-icons/

Compatibility list: http://codex.wordpress.org/Plugins/Plugin_Compatibility/2.7

Hope this helps! M

McGirl