0

I have the following code for creating pagination for a custom taxonomy I created ("custom-tax"). I keep getting a 404 when I click the link for the next page. Any ideas why this is happening?

  $time = current_time('timestamp');

  // Edit:
  $taxonomy = 'custom-tax';
  $number   = 3; // number of terms to display per page

  // Setup:
  $page         = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $offset       = ( $page > 0 ) ?  $number * ( $page - 1 ) : 1;
  $totalterms   = wp_count_terms( $taxonomy );
  $totalpages   = ceil( $totalterms / $number );

  // Debug:
  printf( 'taxonomy: %s - number: %s - page: %s - offset: %s - totalterms %s - totalpages: %s' , $taxonomy, $number, $page, $offset, $totalterms, $totalpages );

  $cat_terms = get_terms('custom-tax', array(
    "hide_empty" => false,
    'orderby'        => 'meta_value',
    'meta_key'       => 'custom_tax_start_date',
    'meta_value'     => $time,
    'meta_compare'   => '>=',
    'order'          => 'DESC',
    'exclude'        => array(),
    'exclude_tree'   => array(),
    'include'        => array(),
    'number'         => $number,
    'fields'         => 'all',
    'slug'           => '',
    'parent'         => '',
    'hierarchical'   => true,
    'child_of'       => 0,
    'get'            => '',
    'name__like'     => '',
    'pad_counts'     => false,
    'offset'         => $offset,
    'search'         => '',
    'cache_domain'   => 'core'
    )
  );

  foreach ($cat_terms as $series_term) {
      //Content
  }

printf( '<nav class="pagination">%s</nav>',
          custom_page_navi( $totalpages, $page, 3, 0 )
        ); ?>

Function:

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

    return paginate_links( array(
        'base'          => str_replace( $bignum, '%#%', esc_url( get_pagenum_link( $bignum ) ) ),
        'format'        => '',
        'current'       => max( 1, $page ),
        'total'         => $totalpages,
        'prev_text'     => 'Prev',
        'next_text'     => 'Next',
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}
  • What page do you have this code on? That page apparently has no second page. Your template never loads in that case. – Milo Jul 20 '17 at 15:44
  • I have this code on a page called "recent-series". It successfully loads the first 3 terms but when I click "page-2", it reaches a 404. I'm not sure if it's a permalink issue or not? – bhood Jul 20 '17 at 15:48
  • I think the issue is how I'm using my templates. I noticed when I put the same code on the index.php file and make a Post, the page link for page 2 works and does not return a 404 error. I'm not sure how to fix this... – bhood Jul 20 '17 at 18:00
  • Whether or not a page is a 404 is based on the main query, which runs before the template is loaded, what happens in the template is irrelevant. – Milo Jul 20 '17 at 21:36
  • I wonder why it works within the index.php file though when it's the same exact code? – bhood Jul 21 '17 at 13:25

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.