1

I've got a custom loop for which I'd like pagination.

The codex and examples I've found online indicate that I should get pagination as long as I pass the 'paged' query variable into the loop as the 'page' variable ( or 'page' for a static front page). However in my case this simply doesn't work.

I'm not working with a static front page: just a normal Page.

What I'm seeing is that for every page that I navigate to, I get the same posts.

This is the template I'm working with:

<div class="container latest-posts group">
    <div class="row">

    <div class="container">     
            <header class="section-header row">
                <div class="col-xs-12">
                    <h2 "><?php the_title()  ?></h2>
                </div>
            </header>
        </div>

        <section id="latest-posts" class="col col-xs-12 col-sm-8 js-masonry">
            <?php
            /*
             *  latest posts
             */
        if ( get_query_var('paged') ) $paged = get_query_var('paged');
            if ( get_query_var('page') ) $paged = get_query_var('page');



            $args = array(
                'posts_per_page' => 7,
                    'offset'    => '5',
                    'paged' => $paged
            ) ;

            $my_query = new WP_Query($args) ;

            while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <article class="frontpage-news-article">
                <div class="inner">
                    <h3>
                        <a href="<?php the_permalink()  ?>">
                            <?php the_title()  ?>
                        </a>
                    </h3>

                    <?php   include( get_stylesheet_directory() . "/templates/post-meta.php") ;   ?>

                    <p><?php  the_excerpt(); ?></p>
                    <p><a href="<?php echo get_permalink()  ?>">read more</a></p>
                </div>
            </article>
            <?php endwhile; ?>

        <?php  
        // Reset postdata
        wp_reset_postdata(); 
        ?>

            <footer class="paging group">
                <?php next_posts_link('&laquo; Older Entries') ?>
                <?php previous_posts_link('Newer Entries &raquo;') ?>

                <?php
                    wp_pagenavi( array(
                        'query' =>$my_query )
                    )
                ?>
            </footer>

        </section> <!-- ENDS #latest-posts  -->

        <aside class="sidebar col-xs-12 col-sm-4 group" role="complementary">
            <ul>
                <?php   dynamic_sidebar('primary') ?>
            </ul>
        </aside><!-- .sidebar -->
    <?php 


    ?>
    </div> <!-- ENDS .container -->
</div>

As you can by the code, I've been trying both wordpress's native pagination calls and also wp-pagenavi. Both of them yield the same undesirable results.

2

1 Answer 1

1

By seeing your code, you are creating the $args array but not passing it to the WP_Query. Try using the below way.

$args = array(
     'posts_per_page' => 7,
      'offset'    => '5',
      'paged' => $paged
 ) ;
$my_query =new WP_Query($args);
......
2
  • 1
    Please format your code so that it is easily readable. Jun 24, 2015 at 5:34
  • Thanks for pointing that out. I've added the instantiation. But I'm not getting working pagination.
    – dave dave
    Jun 25, 2015 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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