简体   繁体   中英

Wordpress pagination navigation: how do I make 'Next Page' and 'Previous Page' persist?

I have made a simple 'posts page' navigation for Wordpress and I can't find a way to alter the behaviour of the next_posts_link() and previous_posts_link() so that the button is always visible, regardless of what page is showing.

Here is the menu in its different states:

第1页,共3页


第2页,共3页


第3页,共3页

I understand that not showing the 'Previous'/'Next' buttons on the first/last page is to be expected of this Wordpress function, however I need to overwrite this behaviour so a greyed-out Previous Page/Next Page text is shown on the first/last page respectively.

Any help would be greatly appreciated as to how I can acheive this. Thank you

Here's 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 } ?>

Working solution (but could be better!):

<?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 } ?>

I think you could make something like this :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM