WordPress showing a default image on single.php

I was hoping someone might be able to help with an issue I’m having…

I’m currently building a WordPress site and I’m putting together the posts section. Everything seems ok, when using a featured image on a post, it displays nicely in a thumbnail here: http://5.10.105.45/~learningforsusta/index.php/education-for-sustainable-development/ and then nicely inside the single post here: http://5.10.105.45/~learningforsusta/index.php/efsc-post/example-post-1/

Read More

However, on a post which doesn’t have a featured image specified, it seems to be displaying a default image…

My question is, how can I get rid of the default image, if there is no featured image, I would like it to not display anything…

Here’s the code I’m using to pull it all through:

<div class="container">   
<div class="row">

  <div class="col-md-9">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <div class="page-header">            

        <?php
          $thumbnail_id = get_post_thumbnail_id(); 
          $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);           
        ?>

        <p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>


        <p><em>
          By <?php the_author(); ?> 
          on <?php echo the_time('l, F jS, Y');?>
          in <?php the_category( ', ' ); ?>.
          <a href="<?php comments_link(); ?>"><?php comments_number(); ?></a>
        </em></p>
      </div>

      <?php the_content(); ?>

      <hr>

      <?php comments_template(); ?>

    <?php endwhile; else: ?>

      <div class="page-header">
        <h1>Oh no!</h1>
      </div>

      <p>No content is appearing for this page!</p>

    <?php endif; ?>


  </div>

  <?php get_sidebar( 'blog' ); ?>

</div>

and here’s the code from the functions.php file that relates to the thumbnails…

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size(150, 120, true);

I’m sure I’m missing something simple, but I’ve tried altering things and i just can’t figure it out!

Any help would be greatly appreciated…

Thanks,

Shaun

Related posts

3 comments

    1. Make sure that you don’t have any function attached to post_thumbnail_html filter, take a look at your functions.php (the best option would be to search in a whole wp-content directory, apply *.php filter to reduce result set)

    2. Make sure you don’t have any third-party plugin which can do this behind the scenes, e.g. this one.

  1. You can try by using has_post_thumbnail() function and display with the_post_thumbnail()

    <?php if ( has_post_thumbnail()): // Check if thumbnail exists ?> 
        <p class="featured-image">       
            <?php the_post_thumbnail('post-thumbnails'); ?>
        </p>
    <?php endif; ?>
    
  2. Just check for existence of featured image before outputting it:

    <?php
      if( has_post_thumbnail() ):
          $thumbnail_id = get_post_thumbnail_id(); 
          $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);?>
    
    <p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
    <?php endif;?>
    

Comments are closed.