0

I have a custom post type called "Releases" set up and paginated perfectly using the following code:

The Loop

    <?php
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $args = array( 'post_type' => 'releases', 'posts_per_page' => 3, 'paged' => $paged, 'page' => $paged );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
    ?>

       <!-- Loop Content -->
          <article>
            <h3><?php the_title(); ?></h3>
            <div><?php the_excerpt(); ?></div>
          </article>
        <!-- end Loop Content -->

    <?php endwhile; ?>

       <!-- Pagination -->
    <?php
        if (function_exists('single_view_pagination')) {
            single_view_pagination($loop->max_num_pages,"", $paged);
        }
    ?>
       <!-- end Pagination -->

The Function

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

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

   global $paged;
   if (empty($paged)) {
     $paged = 1;
   }

   if ($numpages == '') {
     global $wp_query;
     $numpages = $wp_query->max_num_pages;
     if(!$numpages) {
         $numpages = 1;
     }
   }

   $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'       => __('&laquo;'),
     'next_text'       => __('&raquo;'),
     'type'            => plain,
     'add_args'        => false,
     'add_fragment'    => ''
   );

   $paginate_links = paginate_links($pagination_args);

   if ($paginate_links) {
     echo "<nav>";
          echo "<ul>";
       echo "<li>" . $paginate_links . "</li> ";
            echo "</ul>";
     echo "</nav>";
   }
 }

The Problem

Now, when using the code above and viewing the Archive of the custom post type, everything works perfectly and the pagination URLs link as followed:

http://localhost/releases/page/2   
http://localhost/releases/page/3

However, when viewing the Single Post of the custom post type, the issue is that the pagination URLs change to:

http://localhost/releases/post-name/page/2  
http://localhost/releases/post-name/page/3  

Is there any way I can update this code or set up an new function in order to get the pagination URLs on the Single Post view to link properly & without the post-name in the URLs like this?

http://localhost/releases/page/1  
http://localhost/releases/page/2  
http://localhost/releases/page/3  

All help is greatly appreciated. Thanks in advance!

Edit: Here's a screenshot diagram showing exactly the issue that I am trying to fix. I need the function to output the page number links for the post type "Releases", instead of outputting the page number links of the current post "Post Name." Help Screenshot

4
  • I'm not sure I understand what you're asking. How can you view a single post if the URLs are the same for every post? The post name is how WordPress knows which single post to view. Maybe you are trying to duplicate the function of next_post_link / previous_post_link?
    – Milo
    Oct 13, 2015 at 20:27
  • Hi @Milo, I am able to view the single post at: localhost/releases/post-name The pagination links at the bottom are the URLs that change.
    – Marz
    Oct 13, 2015 at 22:07
  • The base argument sets the URL. Look at the output of get_pagenum_link and you'll see why you're getting those URLs from the function.
    – Milo
    Oct 14, 2015 at 15:55
  • @Milo, what do you suggest that I do to the code? I need it to output the page number links for the post type "Releases", instead of outputting the page number links of the current post "Post Name."
    – Marz
    Oct 14, 2015 at 17:09

2 Answers 2

0

You need to set the base properly so the function knows what URL to make the pagination links relative to.

Assuming /releases/ is a post type archive, you can try:

'base' => get_post_type_archive_link( 'releases' ) . '%_%',
0

With the help of Milo's code above, I was able find a solution using this:

'base' => get_pagenum_link(1) . '%_%',

Hope this can help out anyone else that might stumble across this problem. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.