• I’m creating a site that displays a ton of billboard locations for a client. I created a custom post type for the billboards(billboard_location) and a custom taxonomy(location) which holds the county that the billboard is in.

    The idea is to click on a link of the County and have WP return a list of all of the billboards. It sounded so simple….. lol

    In researching this I found that only the archive.php file deals with sorting taxonomies, so that is the file I’ve been modifying for my results.

    First I pass a parameter to a session that captures what county the user clicks on. Then I created a custom loop that uses that county and returns the results.

    All this works fine. But a few counties have over a hundred billboards, so I want to paginate the results to only show 12 at a time. This is where the issue is.

    The pagination code breaks the returned posts into groups of 12 and displays the navigation at the bottom. But whenever you click on a page like(i.e. page 3) it starts to redisplay the first set of twelve, shows the first post and then stops.

    I’m on day 2 of fixing this and any help would be greatly appreciated!!
    (here is a link to the live page with the county of York as an example: https://thespotbeta.com/trone-outdoors/index.php/billboard_location/?county=york)

    Here is the Loop:

    //Start the Loop if County is specified
    
    if($county != ''){
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
    $args = array(
    'post_type' => 'billboard_location',
    'location' 	=> $county,
    'posts_per_page' => 12,
    'paged' => $paged);
    $bill_loc_query = new WP_Query($args);
    if ($bill_loc_query->have_posts()) {
    while($bill_loc_query->have_posts() ): $bill_loc_query->the_post();
    get_template_part( 'content-billboard_locations', get_post_format() );
    endwhile;	
    
    if (function_exists(custom_pagination)) {
    custom_pagination($bill_loc_query->max_num_pages,"",$paged);
    }
    } else {
    // If no content, include the "No posts found" template.
    get_template_part( 'content', 'none' );
    }
    session_unset();

    And here is the pagination code:

    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'            => get_pagenum_link(1) . '%_%',
        'format'          => '&page=%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => True,
        'prev_text'       => __('?'),
        'next_text'       => __('?'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
      );
    
      $paginate_links = paginate_links($pagination_args);
    
      if ($paginate_links) {
        echo "<nav class='custom-pagination'>";
          echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
          echo $paginate_links;
        echo "</nav>";
      }
    
    }

  • The topic ‘Custom Archive page using custom Taxonomies Pagination issue’ is closed to new replies.