how to get next/previous post hrefs and titles in wordpress

后端 未结 3 887
刺人心
刺人心 2021-02-04 14:17

It\'s about the view of a single post. I\'m trying to set the links for previous and next blogposts like this way:



        
3条回答
  •  借酒劲吻你
    2021-02-04 15:08

    No need for functions and filters all you need to do is to use get_adjacent_post instead of next_post_link and prev_post_link, Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it's title attribute use this

    $prev_post = get_adjacent_post(false, '', true);
    if(!empty($prev_post)) {
    echo '' . $prev_post->post_title . ''; }
    

    To get next post and it's title attribute use this

    $next_post = get_adjacent_post(false, '', false);
    if(!empty($next_post)) {
    echo '' . $next_post->post_title . ''; }
    

提交回复
热议问题