0
votes

I built a wordpress theme from an existing theme (One Engine Theme). The site is launched and all seems to be working well except pagination. I have tried so many things but failed to get it to work. All the template pages are meant to paginate at some point but no one seem to work. Below is the looping code for the blog page template:

<?php
  $newsposts = get_posts();
  foreach($newsposts as $post) :
  setup_postdata($post); ?>

//the html codes are added here

<?php endforeach; ?>

I want to make post per page to be 4 and also add the default wordpress pagination function:

<?php posts_nav_link(); ?>

The page template here is blog.php, you can see the page in action here: www.kayodeolusoji.net/blog You help will be well appreciated!

2
Do not use get_posts for paginated queries. get_posts legally breaks pagination. Rather use WP_QueryPieter Goosen

2 Answers

1
votes

Try this:

I add some 'paged' atrribute in $args wiht WP_Query

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;        
            $posts_per_page=4;          
            $args = array(          
                'paged' => $paged,
                'posts_per_page'=>$posts_per_page
            );          
            $newsposts = new WP_Query($args);
              while($newsposts->have_posts()) :$newsposts->the_post();          
              echo $post->ID.'<br />';
              endwhile; 
              wp_reset_query();
            global $wp_query;
            $big = 999999999; 
            echo paginate_links( array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => max( 1, get_query_var('paged') ),
                'total' => $newsposts->max_num_pages 
            ) );
0
votes

I was able to fix after several trials. Here is what I did.

<?php
  $temp = $wp_query;
  $wp_query = null;
  $wp_query = new WP_Query();
  $wp_query->query('showposts=4'.'&paged='.$paged);
  while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>     

//the html codes are added here  

<?php endwhile; ?>
<nav>
  <?php //the pagination codes ?>
</nav>

<?php
 $wp_query = null;
 $wp_query = $temp;  // Reset
?>