WordPress Page Navigation: How Can I Save Next Page and Previous Page?

I made a simple page navigation for Wordpress, and I can’t find a way to change the behavior next_posts_link()and previous_posts_link()so that the button is always visible, no matter which page is displayed.

Here is the menu in different states:

Page 1 of 3


Page 2 of 3


Page 3 of 3

I understand that you should not expect the Previous / Next buttons on the first / last page from this Wordpress function, however I need to rewrite this behavior so that the crossed out text of the previous page / next page is displayed on the first / last page, respectively.

Any help would be greatly appreciated for how I can achieve this. Thanks you

Here is the code:

<?php
function pagination_nav() {
  global $wp_query;

  $total_pages = $wp_query->max_num_pages;
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  ?>

  <nav class="pagination" role="navigation">
    <div class="nav-previous">&larr; <?php previous_posts_link( 'Previous Page ' ); ?></div>
    <div class="nav-position">Page <?php echo $paged ?> of <?php echo $total_pages ?></div>
    <div class="nav-next"><?php next_posts_link( 'Next Page' ); ?> &rarr;</div>
  </nav>

<?php } ?>
+4
2

( !):

<?php
  function pagination_nav() {
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>

<nav class="pagination" role="navigation">
  <div class="nav-previous">

    <?php

      if ($paged == 1) {
        echo '<div class="nav-previous-empty">Previous Page</div>';
      } else {
        previous_posts_link( 'Previous Page' );
      }

    ?>

  </div>
  <div class="nav-position">Page <?php echo $paged ?> of <?php echo $total_pages ?></div>
  <div class="nav-next">

    <?php

      if ($paged == $total_pages) {
        echo '<div class="nav-next-empty">Next Page</div>';
      } else {
        next_posts_link( 'Next Page' );
      }

    ?>
  </div>
</nav>
<?php } ?>
+1

, - :

<nav class="pagination" role="navigation">
    <?php $prev_link = previous_posts_link('Previous Page');
         if (!empty($prev_link)) {
        print __('<div class="nav-previous>&larr;' . $prev_link . '</div>', 'domainname');
        }
    ?>
    <div class="nav-position">Page <?php echo $paged ?> of <?php echo $total_pages ?></div>
    <?php $next_link = previous_posts_link('Next Page');
         if (!empty($next_link)) {
        print __('<div class="nav-next>&larr;' . $next_link . '</div>', 'domainname');
        }
    ?>
  </nav>
0

Source: https://habr.com/ru/post/1655075/


All Articles