2

Ok, I've a custom post type page to which I have attached the paginate_links function to navigate through the elements.

If I don't do anything and let WP using whatever mechanism is attached to the archive page, then the paginate_links work fine but the previous link never goes back to the page one.

Changing manually the links works fine.

On the contrary, if I do something like the answer suggested here, overriding the wp_query object, the paginate_links move to the second page but the "current" remains set always on page 1.

CODE

$current = get_query_var('page') ? get_query_var('page') : 1; // Get query var
$record_per_page = 2; // How many faqs to display on each page
$offset = ( $current - 1 ) * $record_per_page;

    $args = array(
        'post_type'         => 'faqs',
        'orderby'           => 'date',
        'order'             => 'desc',
        'page'              => $current,
        'offset'            => $offset,
        'posts_per_page'    => $record_per_page,
    ); 

    $the_query = new WP_Query( $args );
    $temp_query = $wp_query;
    $wp_query = NULL;
    $wp_query = $the_query;

    $row                = 1;
    $total              = ceil(wp_count_posts('faqs')->publish / $record_per_page);

    while ( have_posts() ) : the_post();
        echo get_the_ID();  
    endwhile;

    // Reset postdata
    wp_reset_postdata();

    if ( $total > 1) {

        $paginate_links = paginate_links( array(
            //'base'      => @add_query_arg( 'page', '%#%' ),
            'base'      => '%_%',
            'prev_text' => __('« Indietro'),
            'next_text' => __('Avanti »'),
            'mid_size'  => 5,
            'format'    => '?page=%#%',
            'current'   => $current,
            'total'     => $total
        )); echo $paginate_links;
    }

// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;

Any idea of what's happening?

2
  • Post your code please.
    – s_ha_dum
    Apr 27, 2014 at 18:54
  • Code updated above. Please bear in mind that if I use the "paged" variable I always get a 404 error page. I don't know whether this could be the solution, but I wasn't able to figure out the reason of my 404. Apr 27, 2014 at 19:24

2 Answers 2

1

paginate_links() has what I consider a horrible bug in that it does not work correctly in many cases with the default base argument, or even with the more complicated sample base in the Codex-- 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),. The first link won't get the pagination argument appended, the link defaults to "this page", and that means that you can't get back to the first page.

The solution I've found for getting around that issue is to use an absolute URL in the base-- something like 'base' => get_permalink().'%_%',

4
  • But that's not what it happens to me ... or at least the argument is there, but it's the wrong one. I will give this solution a go and let you know. Apr 28, 2014 at 8:05
  • That is what you describe-- "the paginate_links move to the second page but the "current" remains set always on page 1"
    – s_ha_dum
    Apr 28, 2014 at 12:38
  • No, that's not what I have described ... the "current" get set to 2 ... but the internal link to go back to page 1 show page=2. Apr 28, 2014 at 18:57
  • That sounds like exactly the same thing to me, but no point arguing. If this answer doesn't help, then hopefully someone else will happen by with an answer that does.
    – s_ha_dum
    Apr 28, 2014 at 19:15
1

Ok, the issue seems to be connected with the base and format parameters. I may not able to explain properly technically what has happened. However, from what I see in my archive page, my page parameter is by default "page" and not "paged" as expected.

That said, when using the base parameter set to '%#%' the hash is supposed to take the correct previous or next page number. This make impossible to use the format and tweak the output if required.

On the contrary, using the '%_%' that force WP to use the format parameter setting the '%#%' as part of the value gets WP to always use the current page everywhere.

So the solution I found was to tweak everything in the base parameter like in the following snippet:

$paginate_links = paginate_links( array(
                'base' => preg_replace('/\?.*/', '/', get_pagenum_link()) . '%_%',
                'prev_text' => __('« Indietro'),
                'next_text' => __('Avanti »'),
                'mid_size'  => 5,
                'current'   => $current,
                'total'     => $totalpages
            ));

Your Answer

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

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