WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Aaress
    Member
    Posted 2 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:

    <?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!!!

  2. keesiemeijer
    moderator
    Posted 2 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
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic