0

I'm trying to create a code that will show the a photo if there is featured image otherwise it will just show the page title. This code is in my header as it's near the logo.

When I use this code on a page without a featured image:

<?php

if ( get_post_thumbnail_id() =='' ) {
   echo 'no picture';
    echo 'its id is '.get_post_thumbnail_id();
}
else {
     echo 'there is a thumbnail';
    echo 'its id is '.get_post_thumbnail_id();

}
?>

It returns "there is a thumbnail it's id is -3."

When I run this code on a page without a featured image:

<?php if ( has_post_thumbnail() ) :?>
  <img class="img-responsive logo-main scale-with-grid" src="<?php the_post_thumbnail_url('full'); ?>"/>
  <div class='text-box'>
   <p class='dataNumber title'><?php the_title();?> </p>
  </div>
</div>
<?php else: ?>
  <div class="logo-main scale-with-grid">no postthumb</div>
  <div class='text-box'>
   <p class='dataNumber title'><?php the_title();?> </p>
  </div>
</div>
<?php endif; ?>

IT returns this:

<div class="containerBox">
  <img class="img-responsive logo-main scale-with-grid" src="">
  <div class="text-box">
   <p class="dataNumber title">Careers </p>
  </div>
</div>

If i set a featured image both codes run fine.

Per Jack I tried this with the same result:

if ( has_post_thumbnail(get_the_id()) ) {
 echo 'there is a thumbnail for post '.get_the_id().' its id is '.get_post_thumbnail_id();
}
else {
      echo 'no picture for post '.get_the_id(); 

}

it does display the right page id though.

  • If I'm not mistaken, you are forgetting an endif statement. – Pim Apr 5 '17 at 17:51
  • both codes are running properly. The first doesn't need endif..the second does need it and it has one. – pressword Apr 5 '17 at 17:53
  • Use has_post_thumbnail for the first code too, and feed it with the post's ID. see if it works then. – Jack Johansson Apr 5 '17 at 17:54
  • tried (see update above) but it's still giving the same result. Any other ideas? – pressword Apr 5 '17 at 18:20
0

It seems like you are using it outside the loop, in which case the the result is unpredictable. You must pass it the post id of the post whose featured image you are trying to fetch.

  • thanks for the help. I added the post id above and am getting the same result. Title, page_id all work as well. It just testing for presence of a photo that isn't working. – pressword Apr 5 '17 at 18:24
0

Try this code:

<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
if ( !empty($thumb_url[0])) {
   echo "There is a thumbnail and it's id is: ".$thumb_id;
} else {
     echo 'There is no picture.';
}
?>

For the second code, use:

<?php 
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
if ( !empty($thumb_url[0]) ) : ?>
  <img class="img-responsive logo-main scale-with-grid" src="<?php echo $thumb_url[0]; ?>"/>
  <div class='text-box'>
   <p class='dataNumber title'><?php the_title();?> </p>
  </div>
</div>
<?php else: ?>
  <div class="logo-main scale-with-grid">no postthumb</div>
  <div class='text-box'>
   <p class='dataNumber title'><?php the_title();?> </p>
  </div>
</div>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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