Lee Willis

Post Thumbnails only for Custom Post Types

| 3 Comments

The guys over at getshopped.org are working on a pretty major revision to their WP e-Commerce plugin. Part of this is migrating “products” into custom post types, including using post thumbnails for the product images. One of the problems that has cropped up in testing was where user’s themes didn’t support post thumbnails. The first fix for was for the plugin to force “theme support” for post thumbnails by calling

add_theme_support( 'post-thumbnails' );

This worked, in that it solved the problem – all of the post-thumbnail function calls worked. However, it left a bug, and one that isn’t a software, or a techie bug – it was the worst kind – a bad user experience. Suddenly “posts” and “pages” would allow you to set featured images when authoring, or editing, but because the theme didn’t really support post thumbnails, those images would never show leaving a confused, bewildered user. So, the solution is to enable the support in general, but disable it for specific post types:

function check_thumbnail_support() {
 if (!current_theme_supports('post-thumbnails')) {
   add_theme_support( 'post-thumbnails' );
   add_action('init','remove_posttype_thumbnail_support');
 }
}
add_action('after_setup_theme','check_thumbnail_support',99);

function remove_posttype_thumbnail_support() {
 remove_post_type_support('post','thumbnail');
 remove_post_type_support('page','thumbnail');
}

This does three important things:
1. Check to see if the theme supports thumbnails already – if so, do nothing
2. If not, then turn on post thumbnail support, but also …
3. Remove support from “post” and “page”

The “gotcha” if you’re trying to come up with this yourself is that theme support is for “post-thumbnails”, but post_type_support is for the more generic “thumbnails”. Hope this comes in handy some someone.

3 Comments

Leave a Reply

Required fields are marked *.