0

I managed to display the thumbnails for the previous and next post on my custom post type but I keep getting this error

"Notice: Trying to get property of non-object in C:\xampp\htdocs\tba\wp-content\themes\artikulo-uno\single-news_press.php on line 137"

I tried researching it but found no luck for a fix on my problem. Here is the code I have been working on.

Show thumbnail of previous post

  <?php


  $prev_post = get_previous_post(true);

  $prev_thumbnail = get_the_post_thumbnail ( $prev_post->ID, array(100, 100) );

  previous_post_link( $prev_thumbnail . '%link', '<span class="prev-link"> <span>Previous Post</span> <br/> %title </span>'  );

  ?>

Show thumbnail of next post

      <?php

      $next_post = get_previous_post(true);

      $next_thumbnail = get_the_post_thumbnail ( $next_post->ID, array(100, 100) );

      next_post_link( $next_thumbnail . '%link', '<span class="next-link"><span>Next Post </span><br/> %title </span>' );
       ?>

Screenshot of the error

0

That message is a general PHP error saying that you are trying to use a variable like a PHP object when it is not a PHP object. If you read the docs for get_previous_post() and get_next_post(), you can see that those functions return a post object or null/empty string if the previous/next post couldn't be determined. So, you need to check that you have an object before trying to use it:

$prev_post = get_previous_post(true);
if( is_object( $prev_post ) ) {
    $prev_thumbnail = get_the_post_thumbnail ( $prev_post->ID, array(100, 100) );
    previous_post_link( $prev_thumbnail . '%link', '<span class="prev-link"> <span>Previous Post</span> <br/> %title </span>'  );
}

$next_post = get_next_post(true);
if( is_object( $next_post ) ) {
    $next_thumbnail = get_the_post_thumbnail ( $next_post->ID, array(100, 100) );
    next_post_link( $next_thumbnail . '%link', '<span class="next-link"><span>Next Post </span><br/> %title </span>' );
}

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.