Neon Emmanuel
Neon Emmanuel

Reputation: 163

Wordpress post query not working

I am trying to figure out a way to make the_posts_pagination or any alternative output numeric post navigation for a custom query.

but it doesn't seem to work, I don't know if I am doing anything wrong would appreciate suggestions and solutions thanks in advance

My Code

<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&order=ASC'); ?>

   <div class="main-post-loop">
        <div class="big-thum-section img-is-responsive">
           <?php if ( has_post_thumbnail() ) : ?>
             <?php the_post_thumbnail('small-block-thumb'); ?>
                <?php endif; ?> 
         </div>

        <div class="squiggle-post-meta-section clearfix">
           <h2><a href="<?php echo get_permalink(); ?>"> <?php the_title(); ?> </a></h2>
             <div class="excerpt-post"><?php the_excerpt(); ?></div>
        </div>
        <div class="continue-reading-section">
         <a href="<?php echo get_permalink(); ?>" class="cont-reading"> Continue reading <i class="fa fa-chevron-right"></i></a> 
        </div>
        <div class="squiggly-line"></div>
    </div>
    <?php
        the_posts_pagination( array(
            'mid_size' => 2,
            'prev_text' => esc_html( '&larr;' ),
            'next_text' => esc_html( '&rarr;' ),
            ) ); 
?>   
<?php wp_reset_query(); // reset the query ?>

Upvotes: 0

Views: 736

Answers (2)

eliza
eliza

Reputation: 116

<?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array('post_type' => 'post', 'order' => 'ASC', 'paged' => $paged, 'posts_per_page' => 12 ));
        if( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php the_title();?>
                <?php the_excerpt(); ?>
            <?php endwhile; ?>
            <div class="pagination"><?php my_pagination(); ?></div>
        <?php endif; ?>
<?php wp_reset_query(); ?>

In your functions.php add,

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;

        $big = 999999999; // need an unlikely integer

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
        ) );
    }
endif;

OR Try this: http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/

Upvotes: 1

Amin Abdolrezapoor
Amin Abdolrezapoor

Reputation: 1847

In order to do that there are a few steps you need to do, first of all i suggest you use the wp-pagenavi plugin, it handles lots of things for you.

Any way, i explain both ways, with and without the plugin, first we write our query and set the paged attribute according to paged query var, so when the user navigates to for example page 3, the query filters posts and shows the third page posts:

$paged = (int) ( get_query_var( 'paged' ) ?: ( get_query_var( 'page' )?: 1 ) );
$my_query = new WP_Query( array(
    'posts_per_page' => 10,
    'paged'          => $paged // This is important for pagination links to work
) );

Now if you have decided to use the wp-pagenavi plugin, it's quite easy to make paginations with custom queries, all you need to do is :

<?php if( function_exists('wp_pagenavi') ) wp_pagenavi( array( 'query' => $my_query ) ); ?>   

But if you wish to use the_posts_pagination() function, i'm not sure if it supports custom queries, but since it's using the paginate_links() function , it should work with this arguments

$args = array(
    'current' => max( 1, $paged ), // $paged is what we defined earlier or you can use just get_query_var('paged')
    'total' => $my_query->max_num_pages
)

if it doesn't, you can use the paginate_links() function itself with the same above arguments.

See also : this answer

Upvotes: 1

Related Questions