[Plugin: Media Tags] checking to see whether image is tagged
-
I have “feature images” (aka post thumbnails) associated with some pages. There are two ways they can be displayed — some span two columns (“fullhead”) and some span only a single column (“colhead”). Full heads are displayed in the header template file; column heads in the page template file. I used Media Tags to tag them either “fullhead” or “colhead.”
I had no trouble getting a post thumbnail or displaying it, but was challenged to out an easy way to find how the thumbnails were tagged. So I thought I would share my solution and see if anyone can improve on it.
function printFeatureImage ($type='fullhead')
{
global $post;$tag_ids = array('fullhead'=>116,'colhead'=>117);
// don't print a second feature image
if ( $this->featureImagePrinted ) {
return;
}// is this a page with a feature image (post thumbnail)?
if ( is_singular() && has_post_thumbnail( $post->ID ) ) {// is this feature image the right size (column head or feature head)?
$tags = wp_get_object_terms( get_post_thumbnail_id( $post->ID ), MEDIA_TAGS_TAXONOMY, 'fields=ids' );if ( in_array($tag_ids[$type],$tags) ) {
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
$this->featureImagePrinted = true;
}
}
}The header template calls printFeatureImage(‘fullhead’) and the page template calls printFeatureImage(‘colhead’), which simply returns if a fullhead has already been printed. Note that this code is in a class, which is why the value of $this->featureImagePrinted is saved between function calls.
- The topic ‘[Plugin: Media Tags] checking to see whether image is tagged’ is closed to new replies.