0
<div id="pagination">
    <div class="prev"><?php next_posts_link('&larr;') ?></div>
    <div class="pages">
        <?php
            global $wp_query;
            $big = 9999;
            echo paginate_links(array(
                'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                'format' => '?paged=%#%',
                'total' => $wp_query->max_num_pages,
                'current' => max(1, get_query_var('paged')),
                'show_all' => false,
                'end_size' => 1,
                'mid_size' => 5,
                'prev_next' => false,
                'prev_text' => 'Prev',
                'next_text' => 'Next',
                'type' => 'list'
            ));
        ?>
    </div>
    <div class="next"><?php previous_posts_link('&rarr;') ?></div>
</div>

Hi, I have a pagination loop setup and am choosing to display both the numbers and the prev / next links. However, I'm trying to figure out how I can end the #'s at the most recent 5 articles [1,2,3,4,5] instead of it carrying over to [1,2,3,4,5,6,7]. How would I go about ending it at the most recent 5 articles published on my blog?

"I'm just wanting to edit the div-class pages part so it just ends at 5." - If that makes any sense.

Screenshot

1 Answer 1

1

You could try

'total' => min(5,$wp_query->max_num_pages),

Edit:

In the source code for paginate_links

http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/general-template.php#L1922

the links are displayed in a loop with $n = 1; $n <= $total; $n++ and since %#% is replaced with $n my idea is to limit the number of displayed pagelinks by restricting $total to the minimum of 5 and $wp_query->max_num_pages. Hope this helps ;-)

5
  • Could you explain why this might solve the problem, and have you tested the solution?
    – s_ha_dum
    Commented Feb 26, 2013 at 20:12
  • sure: I looked at the source code for paginate_links and noticed the loop $n = 1; $n <= $total; $n++ and since %#% is replaced with $n, I thought it might limit the number of displayed pagelinks by restricting $total to the minimum of 5 and $wp_query->max_num_pages. At least this was my idea ;-)
    – birgire
    Commented Feb 26, 2013 at 20:43
  • ... as part of your answer. This answer was flagged as "low quality due to length and content" by the software, which I am sure you want to avoid. It is best to provide explanation along with the raw (suggested) solution.
    – s_ha_dum
    Commented Feb 26, 2013 at 20:52
  • ok thanks @s_ha_dum, I just registered few weeks ago so I'm still learning the rules here ;-) I will update the answer.
    – birgire
    Commented Feb 26, 2013 at 20:58
  • Thanks, that worked! :3 I couldn't find anything in the codex about this sort of thing.
    – Monstr92
    Commented Feb 28, 2013 at 21:17

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.