Query & Display WordPress Posts (with Pagination)

July 11, 2018 | by Mihovil Mikulec
wordpress posts pagination code snippet

Last updated: April 2nd, 2020
Tested with: WordPress 5.4


FUNCTIONALITY:

  • Will query and display posts with pagination
  • Should work with any WordPress theme or child-theme
  • Filters through posts, custom post types, categories, tags, authors etc. (general arguments array)
  • Works on posts page (blog), standard pages and template based pages (such as archives or search results)
  • Filters through pagination arguments array



INSTRUCTIONS:

  • Copy and paste the code below into the WP template file you want to edit
  • Tweak the code according to your needs
    <?php

    $currentPage = get_query_var('paged');


    // General arguments

    $posts = new WP_Query(array(
        'post_type' => 'post', // Default or custom post type
        'posts_per_page' => 10, // Max number of posts per page
        'category_name' => 'My category', // Your category (optional)
        'paged' => $currentPage
    ));


    // Top pagination (pagination arguments)

    echo "<div class='page-nav-container'>" . paginate_links(array(
        'total' => $posts->max_num_pages,
        'prev_text' => __('<'),
        'next_text' => __('>')
    )) . "</div>";


    // Content display

    if ($posts->have_posts()) :
        while ($posts->have_posts()) :
            $posts->the_post();
            echo "<div class='post-wrap'>";
            the_title();
            the_excerpt();
            echo "</div>";
        endwhile;
    endif;


    // Bottom pagination (pagination arguments)

    echo "<div class='page-nav-container'>" . paginate_links(array(
        'total' => $posts->max_num_pages,
        'prev_text' => __('<'),
        'next_text' => __('>')
    )) . "</div>";

    ?>



In case you would like to use this snippet on your category or tag template and make it load only posts related to the current taxonomy (category or tag), simply add the following line above the “general arguments” section:

$taxonomy = get_queried_object();

And inside “general arguments” section change

'category_name' => 'My category',

to

'category_name' => $taxonomy->slug,

(or in case of a tag template)

'tag' => $taxonomy->slug,



In case you would like to use this snippet on your search template, simply add the following line above the “general arguments” section:

$taxonomy = get_search_query();

And inside “general arguments” section change

'category_name' => 'My category',

to

's' => $taxonomy,

Soulstudio


Who we are

What started in 2010 as a one-man web development operation is now a small and devoted team of web developers and server administrators.

Our focus is on WordPress hosting and WordPress care services such as speed optimizations, bug fixing and security hardening.

We are located in Croatia (Europe).

Our mission

Our mission is to provide fast, stable, secure and easy to manage hosting environment for your WordPress websites, as well as additional WordPress care services.

We believe that everybody deserves safe, stable and affordable hosting services. That is why our dedication, commitment and constant learning are reflecting that philosophy.