admin管理员组

文章数量:1297044

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 ?

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 ?

Share Improve this question edited Oct 14, 2013 at 12:51 luckyrajiv asked Oct 14, 2013 at 12:16 luckyrajivluckyrajiv 471 gold badge3 silver badges13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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/automatically-set-the-featured-image-in-wordpress/

Something like this?

<?php if ( has_post_thumbnail()):
    the_post_thumbnail();   
else: ?>
<!-- Do default stuff here -->
<?php endif; ?> 

本文标签: functionsShow Featured Image in else statement