WordPress.org

Ready to get started?Download WordPress

Forums

PHP Conditional Tag for Thumbnails (3 posts)

  1. Aaress
    Member
    Posted 1 year ago #

    Hi,

    I'm working on a theme that is displaying thumbnails on the home page. Currently, the theme is set to display the thumbnail if a featured image is added to the post from within WordPress.

    I'd like to modify the code with an additional conditional tag so that if there is NOT a featured image, a default image would display instead.

    If someone could point out how to make this modification, I'd be most grateful!

    Here's the current code for the thumbnail:

    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { /* if post has a thumbnail */ ?>
    					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" class="img-shadow"><?php the_post_thumbnail('half-thumb'); ?></a>
    					<?php } ?>

    Thank you!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/has_post_thumbnail#Examples

    example:

    <?php if (  has_post_thumbnail() ) { /* if post has a thumbnail */ ?>
    					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" class="img-shadow"><?php the_post_thumbnail('half-thumb'); ?></a>
    					<?php } else { ?>
    					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" class="img-shadow"><img src="whateverdefaulturl" class="attachment-half-thumb" alt="" /></a>
    <?php } ?>
  3. Aaress
    Member
    Posted 1 year ago #

    That worked GREAT! Thanks so much!

    I do have another similar question that hopefully you or someone else could 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=' . $post_num . ' '); 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 &raquo;')); ?>
    				</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 &raquo;')); ?>
    				</div><!--story-text-->
    			</div>
    			<?php } ?>
    			</li>
    			<?php endwhile; ?>
    			</ul>

    Thank you!!!

Topic Closed

This topic has been closed to new replies.

About this Topic