Support » Themes and Templates » Display Image Thumbnails Based on Tag in Post

  • Resolved Aaress

    (@aaress)


    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:

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

    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();?>
    			<li>
    			<?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><!--img-contain-->
    				<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 »')); ?>
    				</div><!--story-text-->
    			</div><!--home-story-cat-->
    			<?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 »')); ?>
    				</div><!--story-text-->
    			</div>
    			<?php } ?>
    			</li>
    			<?php endwhile; ?>
    			</ul>

    Thanks!!!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display Image Thumbnails Based on Tag in Post’ is closed to new replies.