[resolved] Display Image Thumbnails Based on Tag in Post (3 posts)

  1. Aaress
    Posted 3 years ago #

    I have a question that hopefully someone can help me with solving. Is it possible to display an image thumbnail based off the tag used in the post?

    For example, the site is showing a list of recent posts. but instead of the thumbnail image displaying the featured image, I want it to automatically show an image associated with a tag.

    I saw an example code here for categories, but don't know how to tweak it for tags:

       if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
       } else { ?>
          <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php }
    } ?>

    Here's the current code that I have on the site, which I'd like to modify to show the tagged image:

    <ul class="recent">
    			<?php $recent = new WP_Query('showposts=5&offset=1&cat=9'); while($recent->have_posts()) : $recent->the_post();?>
    			<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { /* if post has a thumbnail */ ?>
    			<div class="home-story-cat">
    				<div class="img-contain">
    					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" class="img-shadow"><?php the_post_thumbnail('home-thumb'); ?></a>
    				<div class="story-text">
    					<div class="cat-small"><?php the_tags(); ?></div>
    					<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    					<?php content(20, __('Read more »')); ?>
    			<?php } else { ?>
    			<div class="home-story-cat">
    				<div class="story-text-noimg">
    					<div class="cat-small"><?php the_category(); ?></div>
    					<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    					<?php content(20, __('Read more »')); ?>
    			<?php } ?>
    			<?php endwhile; ?>


  2. keesiemeijer
    Posted 3 years ago #

    Try it with a plugin to assign images to post tags: http://wordpress.org/extend/plugins/taxonomy-images/

    And use this in your template file to show the tag images:

    <?php print apply_filters( 'taxonomy-images-list-the-terms', '', array('taxonomy'     => 'post_tag') ); ?>
  3. Aaress
    Posted 3 years ago #

    Thanks! That plugin worked great. I appreciate your help!

Topic Closed

This topic has been closed to new replies.

About this Topic