1

I am a noobie. Building my new theme. I just need to get 3 separate data:

  • $image = direct link to the next post thumb image (just the link)
  • $name = Next post name
  • $url = Next post URL

Looked at previous / next post with thumbnail dosnt work for me....

Thank you for any help)

1 Answer 1

4

You can use get_adjacent_post function for retrieving that data. This function retrieve adjacent post. Can either be next or previous post.

For previous post

<?php

  $prevpost = get_adjacent_post( false, '', true, 'category' );

  $image = get_the_post_thumbnail( $prevpost );
  $name = get_the_title( $prevpost );
  $url = get_permalink( $prevpost );

?>

For next post

<?php

  $nextpost = get_adjacent_post( false, '', false, 'category' );

  $image = get_the_post_thumbnail( $nextpost );
  $name = get_the_title( $nextpost );
  $url = get_permalink( $nextpost );

?>
3
  • I would cache it in a variable $next_post = ..., otherwise it's a little expensive to call it repeatedly. Jun 14, 2015 at 10:14
  • 1
    Originally I did but when I tested it didn't work with the variable. So had to change it. But that could be me only :) Anyway changed it again.
    – Robert hue
    Jun 14, 2015 at 10:44
  • Also wrap $nextpost/$prevpost in an if - if there is no next/previous post, the following get_* functions will return data for the current post. Jun 14, 2015 at 12:27

Your Answer

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

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