1

I'm trying to paginate some custom posts types, and the previous link works, but not the next link. I tried several ways but I'm not able to find the correct one... if anyone could help :)

This is my code :

function livredor_function($atts,$content=''){
    extract(shortcode_atts(array(
        id => ''
    ), $atts));


    ob_start();


    function create_guestbook() {   

        $i = 1;     

        $max_entries_per_page = 0;
        $current_page = (get_query_var('paged')) ? get_query_var('paged') : 0;
        $gbquery = new WP_query("post_type=inliguestbook&showposts=10&paged=" . $current_page);


        while($gbquery->have_posts()) { $gbquery->the_post();
        ?>
            <div class="guestbook">
                <h3><?php the_title(); ?> <span><?php the_time('j F Y') ?></span></h3>
                <?php the_content(); ?>
            </div>
        <?php
            $i++;
        }   

    ?>
    <div class="pagenav">
         <div class="pagnext"><?php next_posts_link('—›', $max_entries_per_page) ?></div>
        <div class="pagprev"><?php previous_posts_link('‹—', $max_entries_per_page) ?></div>    
    </div>
    <?php
    }   

    $gbquery = null; $gbquery = $temp;

    wp_reset_query();


    return create_guestbook() . ob_get_clean();

}

add_shortcode('livredor','livredor_function');  
3
  • There are two sets of functions, firstly previous_posts_link()&next_posts_link() - plural - and secondly previous_post_link()&next_post_link() - singular. You are mixing them. Intentionally? Or is this the source of your problem? May 22, 2015 at 14:01
  • Dear, thanks for that, this was a mistake. However, it didn't fix the problem. But i tried several things and discover this: if i do [code]query_posts("post_type=inliguestbook&showposts=10&paged=" . $current_page); while(have_posts()) { the_post();[/code] instead of [code]$gbquery = new WP_query("post_type=inliguestbook&showposts=10&paged=" . $current_page); while($gbquery->have_posts()) { $gbquery->the_post();[/code] I get my next link, but i get also 10 other posts after...
    – DavidP
    May 22, 2015 at 14:11
  • Instead of $max_entries_per_page do $gbquery->max_num_pages for you next_posts_link() call. For the previous_posts_link() call it is generally not needed. May 22, 2015 at 14:12

1 Answer 1

0

OK, thank you to ialocin, the $gbquery->max_num_pages solve it!

So there is the clean code :

function livredor_function($atts,$content=''){
    extract(shortcode_atts(array(
        id => ''
    ), $atts));


    ob_start();


    function create_guestbook() {   


        $current_page = (get_query_var('paged')) ? get_query_var('paged') : 0;
        $gbquery = new WP_query("post_type=inliguestbook&showposts=10&paged=" . $current_page);     
        while($gbquery->have_posts()) { $gbquery->the_post();

        ?>
            <div class="guestbook">
                <h3><?php the_title(); ?> <span><?php the_time('j F Y') ?></span></h3>
                <?php the_content(); ?>
            </div>
        <?php
        }   

    ?>
    <div class="pagenav">
         <div class="pagnext"><?php next_posts_link('—›', $gbquery->max_num_pages) ?></div>
        <div class="pagprev"><?php previous_posts_link('‹—', $gbquery->max_num_pages) ?></div>  
    </div>
    <?php
    }   


    wp_reset_query();


    return create_guestbook() . ob_get_clean();

}

add_shortcode('livredor','livredor_function');  
2
  • Glad to be of help. See »How to fix pagination for custom loops?« for some optimization you can and probably should do. May 22, 2015 at 14:19
  • Don't do $gbquery->max_num_pages = 0; it is useless and wrong, as the variable will be set by your WP_Query call. Besides where you do it, there it isn't there yet anyway. If you had debugging turned on, then you would have gotten some messages about it. May 22, 2015 at 14:22

Your Answer

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

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