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?

  • You need to show more code. What is $page?
    – vancoder
    Apr 24, 2014 at 23:25

1 Answer 1


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.


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()): ?>
   <a href="<?php the_permalink();?>">
      <?php the_post_thumbnail( 'thumbnail'); ?>
<?php endif; ?>
  • 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, 2014 at 8:02

Your Answer

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

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