47
<!-- query -->
<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = new WP_Query( array(
        'category_name' => 'investor-news',
        'posts_per_page' => 2,
        'paged' => $paged
    ) );
?>

<?php if ( $query->have_posts() ) : ?>

<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" title="Read"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <?php echo get_the_date(); ?>

<?php endwhile; ?>
<!-- end loop -->


<!-- WHAT GOES HERE?????? -->


<?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

I've tried everything to achieve pagination on this static page using the wp_query function but without any luck. There's a comment in this script called WHAT GOES HERE?????... so what goes here?

This is on a static page that is not the front page or the posts page.

lurning too koad
  • 629
  • 2
  • 7
  • 13

4 Answers4

67

Replace <!-- WHAT GOES HERE?????? --> with the pagination code below:

<div class="pagination">
    <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
</div>

WordPress comes with a handy function called paginate_links() which does the heavy lifting. In the example above, the custom WP_Query object $query is used instead of the global $wp_query object.

Edit: In response to comment from @mark-in-motion, here's an example of a wrapper function for paginate_links() that I made a while back. You can add this to your theme's functions.php, then call echo my_pagination(); in your template to display pagination.

/**
 * Numeric pagination via WP core function paginate_links().
 * @link http://codex.wordpress.org/Function_Reference/paginate_links
 *
 * @param array $args
 *
 * @return string HTML for numneric pagination
 */
function my_pagination( $args = array() ) {
    global $wp_query;
    $output = '';
if ( $wp_query-&gt;max_num_pages &lt;= 1 ) {
    return;
}

$pagination_args = array(
    'base'         =&gt; str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        =&gt; $wp_query-&gt;max_num_pages,
    'current'      =&gt; max( 1, get_query_var( 'paged' ) ),
    'format'       =&gt; '?paged=%#%',
    'show_all'     =&gt; false,
    'type'         =&gt; 'plain',
    'end_size'     =&gt; 2,
    'mid_size'     =&gt; 1,
    'prev_next'    =&gt; true,
    //'prev_text'    =&gt; __( '&amp;laquo; Prev', 'text-domain' ),
    //'next_text'    =&gt; __( 'Next &amp;raquo;', 'text-domain' ),
    //'prev_text'    =&gt; __( '&amp;lsaquo; Prev', 'text-domain' ),
    //'next_text'    =&gt; __( 'Next &amp;rsaquo;', 'text-domain' ),
    'prev_text'    =&gt; sprintf( '&lt;i&gt;&lt;/i&gt; %1$s',
        apply_filters( 'my_pagination_page_numbers_previous_text',
        __( 'Newer Posts', 'text-domain' ) )
    ),
    'next_text'    =&gt; sprintf( '%1$s &lt;i&gt;&lt;/i&gt;',
        apply_filters( 'my_pagination_page_numbers_next_text',
        __( 'Older Posts', 'text-domain' ) )
    ),
    'add_args'     =&gt; false,
    'add_fragment' =&gt; '',

    // Custom arguments not part of WP core:
    'show_page_position' =&gt; false, // Optionally allows the &quot;Page X of XX&quot; HTML to be displayed.
);

$pagination_args = apply_filters( 'my_pagination_args', array_merge( $pagination_args, $args ), $pagination_args );

$output .= paginate_links( $pagination_args );

// Optionally, show Page X of XX.
if ( true == $pagination_args['show_page_position'] &amp;&amp; $wp_query-&gt;max_num_pages &gt; 0 ) {
    $output .= '&lt;span class=&quot;page-of-pages&quot;&gt;' .
                                sprintf( __( 'Page %1s of %2s', 'text-domain' ), $pagination_args['current'], $wp_query-&gt;max_num_pages ) .
            '&lt;/span&gt;';
}

return $output;

}

Dave Romsey
  • 17,952
  • 11
  • 56
  • 70
15

This code is for Custom Query Pagination. You can follow the steps to create your own pagination in WordPress.

 <?php
/**
* Template Name: Custom Page
*/
get_header(); ?>

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 4,
  'paged' => $paged
);
$custom_query = new WP_Query( $args );
?>
          <!----start-------->
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php
   while($custom_query->have_posts()) :
      $custom_query->the_post();
?>
       <div>
        <ul>
         <li>
           <h3><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h3>
        <div>
          <ul>
        <div><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a></div>
          </ul>
          <ul>
        <p><?php echo the_content(); ?></p>
          </ul>
        </div>
        <div>
          </li>
        </ul>
          </div> <!-- end blog posts -->
       <?php endwhile; ?>
      <?php if (function_exists("pagination")) {
          pagination($custom_query->max_num_pages);
      } ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
          <!----end-------->
        <?php get_footer();

Reference : https://www.wpblog.com/use-wp_query-to-create-pagination/

suthanalley
  • 151
  • 1
  • 4
3

If anyone arrives here attempting to create a wp_query with pagination from inside a shortcode:


add_shortcode('wp_query_pagination_inside_shortcode', 'my_shortcode_function_tag');
if (!function_exists('my_shortcode_function_tag')) {
    function my_shortcode_function_tag($atts)
    {
        ob_start(); // Turn output buffering on
        global $paged;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $atts = shortcode_atts(
            array(
                'post_type' => 'post',
                // whatever you like
            ),
            $atts
        );
        $args = array(
            'posts_per_page' => 3,
            'paged' => $paged,
            'post_type' => $atts['post_type'],
            'orderby' => 'date',
            'order' => 'DESC',
        );
        $the_query = new WP_Query($args);
    if ($the_query-&gt;have_posts()) {
        while ($the_query-&gt;have_posts()) {
            $the_query-&gt;the_post();
            get_template_part('template-parts/loop');
        } //end while

        $big = 999999999; // need an unlikely integer
        echo paginate_links(
            array(
                'base' =&gt; str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
                'format' =&gt; '?paged=%#%',
                'current' =&gt; max(
                    1,
                    get_query_var('paged')
                ),
                'total' =&gt; $the_query-&gt;max_num_pages //$q is your custom query
            )
        );
    } // Pagination inside the endif
    return ob_get_clean(); // Silently discard the buffer contents
    wp_reset_query(); // Lets go back to the main_query() after returning our buffered content
}

}

1

You can simply use a built-in WP function

<?
the_posts_pagination()
?>

It does the job very well with custom WP_Query

iBazzo
  • 19
  • 2