1

I inherited this WordPress project and it is part of a live Website. The original author wrote a custom pagination function that is being used more than once on the Website. However, on a few pages, it simply doesn't work.

This is the function (it's ugly, but it looks fine to me)

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   *
   * It's good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /**
   * We construct the pagination arguments to enter into our paginate_links
   * function.
   */
  $pagination_args = array(
    'base'            => '%_%',
    'format'          => '?paged=%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('«'),
    'next_text'       => __('»'),
    'type'            => 'array',
    'add_args'        => true,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if (is_array($paginate_links)) {
    echo '<ul class="pagination">';
    foreach ( $paginate_links as $page ) {
      echo "<li>$page</li>";
    }
    echo '</ul>';
  }
}

and is being called like this everywhere it is used

<?php custom_pagination($query->max_num_pages,"",$paged); ?>

The links being constructed look fine

<ul class="pagination">
  <li><span class="page-numbers current">1</span></li>
  <li><a class="page-numbers" href="?paged=2">2</a></li>
  <li><a class="page-numbers" href="?paged=3">3</a></li>
  <li><a class="next page-numbers" href="?paged=2">»</a></li>
</ul>

However, clicking on page 2 or 3 redirects to http://domain.com/paged/2/, for example. Then, the generated pagination looks like this :

<ul class="pagination">
  <li><a class="prev page-numbers" href="">«</a></li>
  <li><a class="page-numbers" href="">1</a></li>
  <li><span class="page-numbers current">2</span></li>
  <li><a class="page-numbers" href="?paged=3">3</a></li>
  <li><a class="next page-numbers" href="?paged=3">»</a></li>
</ul>

Notice how the previous pages links are wrong? How do I fix this?

update

Also, when hovering over the first page link, the URL shows http://domain.com/paged/2/ (i.e. because of href=""), but hovering over page 3, it shows http://domain.com/paged/2/?paged=3.

That's not right.

Update 2

Before you mention it, yes, I did update the permalinks, many times.

  • Since it only happens on a few pages, it may be a product of the original author not taking into account the page query var in addition to paged. What does the logic which sets up the $paged variable that's passed to the function look like? – bosco Mar 27 '17 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.