Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get the thumbnail of my featured image to display on my home.php (blog) page.

It is displaying fine, but when debugging mode is turned on, I get this error:

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/wpflat/wp-content/themes/wpflat/content-blog.php on line 2

My code is

<a href="<?php the_permalink();?>"><?php echo get_the_post_thumbnail($page->id, 'thumbnail'); ?></a>

How do I solve this issue?

share|improve this question
    
You need to show more code. What is $page? –  vancoder Apr 24 at 23:25
add comment

1 Answer 1

up vote 1 down vote accepted

I believe from your error that you are using this code inside the loop. You should be using the_post_thumbnail(). The code you are using is used outside the loop.

EDIT

It is always good practice to always first check if you have a thumbnail to display

So you should use

<?php if(has_post_thumbnail( 'thumbnail')): ?>
   <a href="<?php the_permalink();?>">
      <?php the_post_thumbnail( 'thumbnail'); ?>
   </a>
<?php endif; ?>
share|improve this answer
    
Great. Your code worked. Thanks a lot!! The image was in the loop, however the issue was that I added $page->id to the code when I should have left it out. That is why it came with the error "Trying to get property of non-object" –  user50763 Apr 25 at 8:02
    
Exactly as I pointed out. Please mark my answer as accepted then –  Pieter Goosen Apr 25 at 8:12
    
How do I do that? –  user50763 Apr 26 at 15:09
    
Just click on the chech mark below the up and down arrows next to my answer –  Pieter Goosen Apr 26 at 15:48
1  
Super, thanks! Done that! –  user50763 Apr 27 at 19:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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