Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

probably a rather straight forward question.

my "homepage" is not the blog-page. I have a page-template named page-knowledge.php which has the following loop on it.

<?php
        echo '<ul class="blog-items">';

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 5,
        );

        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
            global $post;
            get_template_part( 'inc/post', 'item' );
        endwhile;

        wp_reset_postdata();
        echo '</ul>';
    ?>

So, this page queries 5 of the normal posts. How can I add a pagination to this page so I can jump through the rest of the posts just like you would expect it.


UPDATE

<?php
        echo '<ul class="blog-items">';

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 5,
        );

        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
            global $post;
            get_template_part( 'inc/post', 'item' );
        endwhile;

        wp_reset_postdata();
        echo '</ul>';
    ?>

    <div class="pagination wrapper">
        <?php 
            $endsize = $midsize = 1;
            $type = 'plain';
            $loop->query_vars['paged'] > 1 ? $current = $loop->query_vars['paged'] : $current = 1;

            // Sanitize input argument values
            if ( ! in_array( $type, array( 'plain', 'list', 'array' ) ) ) $type = 'plain';
            $endsize = (int) $endsize;
            $midsize = (int) $midsize;

            // Setup argument array for paginate_links()
            $pagination = array(
                'base'          => @add_query_arg('paged','%#%'),
                'total'         => $loop->max_num_pages,
                'current'       => $current,
                'show_all'      => false,
                'end_size'      => $endsize,
                'mid_size'      => $midsize,
                'type'          => $type,
                'prev_next'     => false,
                'paged'         => $loop->query_vars['paged']
            );

            echo paginate_links( $pagination );
        ?>
    </div>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

get_pagination_links is looking at $wp_query, whereas you want pagination for your query object named $loop. So instead, use paginate_links

Something similar to this code, which is based on get_pagination_links, alebit with wp-query swapped out for your loop query object:

$endsize = $midsize = 1;
$type = 'plain';
$loop->query_vars['paged'] > 1 ? $current = $loop->query_vars['paged'] : $current = 1;

// Sanitize input argument values
if ( ! in_array( $type, array( 'plain', 'list', 'array' ) ) ) $type = 'plain';
$endsize = (int) $endsize;
$midsize = (int) $midsize;

// Setup argument array for paginate_links()
$pagination = array(
    'base'          => @add_query_arg('paged','%#%'),
    'total'         => $loop->max_num_pages,
    'current'       => $current,
    'show_all'      => false,
    'end_size'      => $endsize,
    'mid_size'      => $midsize,
    'type'          => $type,
    'prev_next'     => false,
    'paged'         => $loop->query_vars['paged']
);

echo paginate_links( $pagination );

Finally you'll need to grab the paged URL variable via $_GET, do a tad sanitisation ( check it's a number and it's set ), then put it into your WP_Query args array

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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