mfields/Taxonomy-Images — Issue #7: Some functions you may appreciate
Author: davidcasey
Created: 2012-01-18 17:58:19+00:00
State: open

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.

https://github.com/davidcasey

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:

$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() );

/**
* Retrieve Post Taxonomy Thumbnails
* If post has taxonomy tags and if thumbnail exists, add to the array.
* Returns an array of thumbnail objects: term id, attachment id, source, width, height
*
* Requires: taxonomy-images plugin
*
* @param int $post_id Optional. Post ID.
* @param string $size Optional. Image size. Defaults to 'category-thumb'.
* @param array $taxonomies Optional. Array of taxonomy names to use, in that order.
*
* @return array Array of Objects containing the term_id, attachment_id, and src
*/
function get_post_taxonomy_thumbnails($post_id = null, $size = 'category-thumb', $taxonomies = null ) {
global $taxonomy_images_plugin;
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
$size = apply_filters('post_thumbnail_size', $size);
$taxonomies = ( null === $taxonomies ) ? get_post_taxonomies() : $taxonomies;
$taxonomy_thumbnails = array();

foreach ( $taxonomies as $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy);
if ($terms) foreach ( $terms as $term ) {
$term_taxonomy_id = $term->term_taxonomy_id;

if( isset( $taxonomy_images_plugin->settings ) ) {
if( array_key_exists( $term_taxonomy_id, (array) $taxonomy_images_plugin->settings ) ) {

$attachment_id = $taxonomy_images_plugin->settings[$term_taxonomy_id];
$src = wp_get_attachment_image_src( $attachment_id, $size );

$taxonomy_thumbnails[] = (object) array(
'term_id' => $term_taxonomy_id,
'attachment_id' => $attachment_id,
'src' => $src[0],
'width' => $src[1],
'height' => $src[2]
);
}
}
}
}
return $taxonomy_thumbnails;
}

/**
* Retrieve Post Taxonomy Thumbnail
*
* This function was created to mimic get_the_post_thumbnail() in
* /wp-includes/post-thumbnail-template.php allowing for actions and filters
* to work as expected.
*
* @param int $taxonomy_thumbnail_id. The id of the thumbnail to retrieve.
* @param string $size Optional. Image size. Defaults to 'thumbnail'.
* @param string|array $attr Optional. Query string or array of attributes.
* @param int $post_id Optional. Post ID.
*/
function get_the_post_taxonomy_thumbnail($taxonomy_thumbnail_id, $size = 'category-thumb', $attr = '', $post_id = null ) {
$size = apply_filters('post_thumbnail_size', $size);
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;

do_action( 'begin_fetch_post_thumbnail_html', $post_id, $taxonomy_thumbnail_id, $size ); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
if ( in_the_loop() )
update_post_thumbnail_cache();
$html = wp_get_attachment_image($taxonomy_thumbnail_id, $size, false, $attr );
do_action( 'end_fetch_post_thumbnail_html', $post_id, $taxonomy_thumbnail_id, $size );

return apply_filters( 'post_thumbnail_html', $html, $post_id, $taxonomy_thumbnail_id, $size, $attr );
}

/**
* Display Post Taxonomy Thumbnail
*
* This function was created to mimic the_post_thumbnail() in
* /wp-includes/post-thumbnail-template.php allowing for actions and filters
* to work as expected.
*
* @param int $taxonomy_thumbnail_id. The id of the thumbnail to retrieve.
* @param string $size Optional. Image size. Defaults to 'thumbnail'.
* @param string|array $attr Optional. Query string or array of attributes.
*/
function the_post_taxonomy_thumbnail($taxonomy_thumbnail_id, $size = 'category-thumb', $attr = '' ) {
echo get_the_post_taxonomy_thumbnail($taxonomy_thumbnail_id, $size, $attr, null);
}