0

This is how i am loading the post thumbnail image ?

What if there is no image, i want to show a default image ?

How can i show that.

Here is the code i use to display thumbnail.

        $html .= '<div class="post event" id="post-'.$post->ID.'">

                    <a href="'.get_permalink($post->ID).'"> '.get_the_post_thumbnail().' </a>

                    <div class="content">
                        <span class="title">
                            <a title="'.$post->post_title.'" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>
                        </span>                                             
                        <div class="addl-123">';                            

                    $terms = wp_get_post_terms($post->ID, 'sublocation');
                    if($terms)
                    {
                        $out = array();
                        foreach ($terms as $term)
                        {
                            $out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'sublocation') .'">' .$term->name .'</a>';
                        }
                        //echo join( ', ', $out);
                        $html .= join( ', ', $out);
                    }

                    if($is_parent != 0)
   {
    $address = get_post_meta($is_parent,'address',true);
         }
   else
   {
    $address = get_post_meta($post->ID,'address',true);
         }
         if(!$address){ $address='-'; }
   if($address && $address!="-"){$html .= "<b>".$address."</b>";}

    $html .= '<p>Timinigs: '.get_post_meta($post->ID, 'cstimings', true). ' </p>';

        $html .= '</div>

On line no 2 of code, i am able to display thumbnail of posts who have featured images. but for other posts there are no featured images. so, for such posts i want to display default image.

How can i do that ?

3 Answers 3

2

The featured image-related functions do not offer any way to define a default/fallback image. So, you'll have to define one yourself, e.g.

<?php $featured_image = ( '' != get_the_post_thumbnail() ? get_the_post_thumbnail() : get_template_directory_uri() . '/images/img_not_available.png' ); ?>

<a href="<?php the_permalink(); ?>"><?php echo $featured_image; ?></a>

Edit

Three changes:

  1. More explicit conditional. get_the_post_thumbnail() returns an empty string rather than false if no image is found.
  2. Updated the default image with yours. Note: use get_template_directory_uri() and not get_stylesheet_directory_uri().
  3. Cleaned up interspersed PHP/HTML, to help eliminate syntax errors

Note also: example code assumes a context of inside the loop. Modify accordingly if it is used outside the loop.

8
  • not working . its showing blank page after this
    – luckyrajiv
    Oct 14, 2013 at 12:41
  • Then you have a syntax error somewhere. This is just example/proof-of-concept code, that you will need to adapt to your own needs. Also: where is your default image? Oct 14, 2013 at 12:42
  • i will edit and paste the whole code. I just added the whole code. please see line no 2 in the code it is used for displaying the thumbnail.
    – luckyrajiv
    Oct 14, 2013 at 12:43
  • my default image is here get_stylesheet_directory_uri()."/images/img_not_available.png"
    – luckyrajiv
    Oct 14, 2013 at 12:45
  • See edited answer. Oct 14, 2013 at 12:51
0

This code works from your child themes functions file.

On top of this you can also set a default fallback in case there are no images in a post:

    function wpforce_featured() {
      global $post;
      $already_has_thumb = has_post_thumbnail($post->ID);
          if (!$already_has_thumb)  {
          $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                      if ($attached_image) {
                            foreach ($attached_image as $attachment_id => $attachment) {
                            set_post_thumbnail($post->ID, $attachment_id);
                            }
                       } else {
                            set_post_thumbnail($post->ID, '414');
                       }
                    }
  }  //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

Source http://wpforce.com/automatically-set-the-featured-image-in-wordpress/

-1

Something like this?

<?php if ( has_post_thumbnail()):
    the_post_thumbnail();   
else: ?>
<!-- Do default stuff here -->
<?php endif; ?> 
1
  • No not this one. i want something like above.
    – luckyrajiv
    Oct 14, 2013 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.