0

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>

1 Answer 1

1

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

0

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.