Open davidcasey opened 12 years ago
I appreciate them like crazy.
Thanks. One more for everyone. This function will get and return the thumbnail of the taxonomy in the same manner as the get_post_taxonomy_thumbnails function does. That way you can work with results from either in the same way.
/** * Get Taxonomy Thumbnail * * Requires taxonomy-images plugin * * @param int $term_tax_id Optional. Taxonomy Term ID. * @param string $size Optional. Image size. Defaults to 'category-thumb'. * * @return array Array of Objects containing the term_id, attachment_id, and src */ function get_taxonomy_thumbnail($term_tax_id = 1, $size = 'category-thumb') { global $taxonomy_images_plugin; if ( isset( $taxonomy_images_plugin->settings ) ) { if ( array_key_exists( $term_tax_id, (array) $taxonomy_images_plugin->settings ) ) { $image_id = $taxonomy_images_plugin->settings[$term_tax_id]; $src = wp_get_attachment_image_src( $image_id, $size ); return (object) array( 'term_id' => $term_tax_id, 'attachment_id' => $image_id, 'src' => $src[0], 'width' => $src[1], 'height' => $src[2] ); } } }
Want to see this a function calling these guys?
I needed to get a thumbnail for "the loop." First, I wanted the post thumbnail if it existed, then the taxonomy thumbnail in the correct hierarchical order if they existed, and finally default to the uncategorized taxonomy thumbnail when all else fails.
$taxonomies = array('foo', 'bar', 'baz'); function childtheme_get_the_post_thumbnail($size = null, $attr = null) { global $taxonomies; if ( !$attr[title] ) $attr = array( 'title' => ''); if ( has_post_thumbnail() ) { return get_the_post_thumbnail(get_the_ID(), $size, $attr); } else { $taxonomy_thumbnails = get_post_taxonomy_thumbnails( get_the_ID(), $size, $taxonomies ); $thumb = get_the_post_taxonomy_thumbnail( $taxonomy_thumbnails[0]->attachment_id, $size, $attr, get_the_ID() ); // If post has taxonomy tags and if thumbnail exists, use first found image. Else default (uncategorized) return $thumb ? $thumb : get_the_post_taxonomy_thumbnail(get_taxonomy_thumbnail(1, $size, $attr)->attachment_id, $size, $attr); } }
Thanks for the plugin. Here are some functions I have written that you may be interested in using. They mostly follow the existing WP functionality for post thumbnails.
Regarding get_post_taxonomy_thumbnails having a $taxonomies parameter: This allows a custom sort and eliminates unwanted taxonomies as well, e.g. If I only want to print one taxonomy thumbnail, I can have a hierarchy of preference which thumb gets printed: