i know that : <?php $pagename = get_query_var('name'); ?> get the name of the current page/post. But I dont know how to get the name of the next and the previous post. I will use it for a hover tip and the alt of the image that will lead to the previous/next post

1 Answer 1

$prev = get_previous_post();
$next = get_next_post();
$prev_title = $prev ? get_the_title($prev) : 'Current is First Post';
$next_title = $next ? get_the_title($next) : 'Current is Last Post';

See get_previous_post and get_next_post on Codex for some details and additional params you can use.

  • it work, but please explain me the notation ... i dont know this short way of writing php... please make the long version so i can learn... thanks
    – menardmam
    Commented Aug 31, 2013 at 0:51
  • $prev_title = $prev ? get_the_title($prev) : 'Current is First Post'; is the same of if ($prev) $prev_title = get_the_title($prev); else $prev_title = 'Current is First Post'; For more info see, e. g. here.
    – gmazzap
    Commented Aug 31, 2013 at 1:20

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.