mfields / Taxonomy-Images

Enables WordPress users to easily associate images from the media library to categories, tags and custom taxonomies.
http://wordpress.mfields.org/plugins/taxonomy-images/
52 stars 21 forks source link

Some functions you may appreciate #7

Open davidcasey opened 12 years ago

davidcasey commented 12 years ago

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:

    $taxonomies = array('foo', 'bar', 'baz');
    $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);
}
topher1kenobe commented 12 years ago

I appreciate them like crazy.

davidcasey commented 12 years ago

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);
  }
}