Home » Wordpress » Paginate posts in WordPress Theme

Paginate posts in WordPress Theme

Posted by: admin November 30, 2017 Leave a comment

Questions:

Alright, I’ve been staring at this for nearly an hour now and can’t get it to work. I’m using WordPress and I have 12 posts in the system. In the admin, I set it to display 5 posts per page. In my theme, I am trying to display pagination (prev,1,2,3,4,next) at the bottom. I found the paginate_links() function on the WordPress guides and it doesn’t print anything out… Any help is appreciated:

<?php get_header(); ?>
<div class="middle-container">
    <div class="middle">
        <div class="column main">
            <div class="latest">
            <?php
            $i = 0;
            if (have_posts()):
                while (have_posts() && $i < 1):
                    the_post();
                    ?>
                    <div class="image"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <div class="content">
                    <h1><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                    <span class="date"><?php the_time('F jS, Y') ?></span>
                    <?php
                    the_excerpt();
                    ?>
                    </div>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
            </div>
            <ul class="posts">
            <?php
            $i = 0;
            if (have_posts()):
                while (have_posts() && $i < 4):
                    the_post();
                    ?>
                    <li>
                    <h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <span class="date"><?php the_time('F jS, Y') ?></span>
                    <?php
                    the_excerpt();
                    ?>
                    </li>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
            </ul>
            <?php
            echo paginate_links();
            ?>
        </div>
        <div class="column sidebar"></div>
    </div>
</div>
<?php get_footer(); ?>

A little more background on this: The first while loop in the code grabs the first post in the list and displays it in its own special block. The second while loop grabs the remaining 4 posts. Both of these while loops work perfectly fine. The pagination just simply isn’t printing out.

Answers:

It appears that the WordPress paginate_links function expects one argument.

I would try their default example and see if it works.

<?php
paginate_links(array(
    'base'         => '%_%',
    'format'       => '?page=%#%',
    'total'        => 1,
    'current'      => 0,
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('&laquo; Previous'),
    'next_text'    => __('Next &raquo;'),
    'type'         => 'plain',
    'add_args'     => False,
    'add_fragment' =>  ));
?>

Questions:
Answers:

Found the answer to my solution. If you leave the “total” as 1, the function does nothing. You have to put the quantity of pages in the “total” parameter. I would think that WordPress would be smart enough to make that function grab the number of posts from the collection…

Anyways – anyone else that is looking for pagination functionality that doesn’t require a lot of custom code (like paginate_links would), check this out:
http://wordpress.org/extend/plugins/wp-paginate/

Seems to work pretty well.