How to display links to the previous and next post in the category?

Question

How to loop the output of posts when I click on the “Forward” or “Back” buttons so that there is a transition in the same category as the previous post. And now, when posts of a certain category end, then he also shows other posts of other categories, but I need to, if posts of a certain category have ended, then pressing the “forward” button will display the first post of this category, if the post is the first and pressing the button “Back” then went to the last post in this category.

  if( get_adjacent_post(true, '', true) ) {
      previous_post_link('%link', '<div class="icon-button"></div> ');
  } else {
      $first = new WP_Query('post_type=portfolio&posts_per_page=1&order=DESC'); $first->the_post();
      echo '<a href="' . get_permalink() . '"></a>';
      wp_reset_query();
   };

   if( get_adjacent_post(false, '', false) ) {
        next_post_link('%link', '<div class="icon-button"></div>');
   } else {
        $last = new WP_Query('post_type=portfolio&posts_per_page=1&order=ASC'); $last->the_post();
        echo '<a href="' . get_permalink() . '"></a>';
        wp_reset_query();
   };
0
, , Kaizer 2 years 2019-12-26T02:14:59-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse