Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a blog page at: mysite.com/blog. On this page I have this loop:

<?php

$args = array (
  'pagination'             => true,
  'paged'                  => 'pages',
  'posts_per_page'         => '5',
  'ignore_sticky_posts'    => true
 );
?>

<?php $query = new WP_Query( $args ); ?>

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

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<?php the_title('<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '" rel="bookmark">', '</a></h2>'); ?>

<p class="byline">
  <span class="author vcard"><?php the_author_posts_link(); ?></span> <span class="sep">|</span> 
  <abbr class="published" title="<?php the_time(__('l, F jS, Y, g:i a', 'example')); ?>"><?php the_time(__('F j, Y', 'example')); ?></abbr>
  <?php edit_post_link(__('Edit', 'example'), ' <span class="sep">|</span> <span class="edit">', '</span> '); ?>
</p>

<div class="entry-content">
  <?php the_content(); ?>
  <?php wp_link_pages('before=<p class="pages">' . __('Pages:','example') . '&after=</p>'); ?>
</div>

<p class="entry-meta">
  <span class="categories"><?php _e('Posted in', 'example'); ?> <?php the_category(', '); ?></span>
  <?php the_tags('<span class="tags"> <span class="sep">|</span> ' . __('Tagged', 'example') . ' ', ', ', '</span>'); ?> 
  <span class="sep">|</span> <?php comments_popup_link(__('Leave a response', 'example'), __('1 Response', 'example'), __('% Responses', 'example'), 'comments-link', __('Comments closed', 'example')); ?> 
</p>

Which works as expected, however I cannot get pagination to work. I have tried a variety of options. Any help is appreciated.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

This works for me with no 404s

 <?php
 $args = array (
    'posts_per_page' => 5 ,
    'paged' => $paged,
    'ignore_sticky_posts'    => true
  );
  $blog_query = new WP_Query($args);
  if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link('&larr; Older posts', $blog_query->max_num_pages); 
     previous_posts_link('Newer posts &rarr;', $blog_query->max_num_pages); 
  } 
  while ( $blog_query->have_posts() ) {
      $blog_query->the_post();

     // STUFF
  }
     if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link('&larr; Older posts', $blog_query->max_num_pages); 
     previous_posts_link('Newer posts &rarr;', $blog_query->max_num_pages); 
  }
  wp_reset_query();
  ?>

I can see the need for a secondary loop if you want to not use category pages or want there to be editable content on the page or a title different than the category name.

share|improve this answer
    
This alone is almost certainly not going to work because this is a secondary loop and the ordinary pagination functions do not work well with secondary loops, plus the main query will be out of sync with the secondary query so you are likely to get 404s. –  s_ha_dum Jun 28 '13 at 19:25
    
I added some info –  eteich Jun 28 '13 at 20:11
    
next_posts_link assumes the main query, so are paginating on a different query than you are using to display the posts. The same is true with previous_posts_link. There will be problems because of that. –  s_ha_dum Jun 28 '13 at 20:22
    
I'm specifying the blog query in it –  eteich Jun 28 '13 at 20:26
    
This seems to be working for me so far. –  Scott Simpson Jun 28 '13 at 20:41
show 4 more comments

You should not need to create a secondary Loop. Besides causing additional work for the server, the ordinary pagination functions do not work well with secondary loops.

Use pre_get_posts to alter the main query.

function pregp_wpse_104648($qry) {
  if (is_main_query() && $qry->is_home()) {
    $qry->set('posts_per_page','5');
    $qry->set('ignore_sticky_posts',true);
  }
}
add_action('pre_get_posts','pregp_wpse_104648');

I believe that is all you need.

Note: I am guessing at the conditions you need. if (is_main_query() && $qry->is_home()) { may be wrong, but that is the idea.

share|improve this answer
    
If I don't create the loop, my posts don't display. All I get is the title of the page (where it should be the title of the post). I am using a custom template (blog.php) and under settings/reading posts page is set to 'blog'. –  Scott Simpson Jun 28 '13 at 19:59
    
You need the Loop, what you don't need is a secondary Loop-- the new query--, unless you are doing something odd with the blog index. –  s_ha_dum Jun 28 '13 at 20:03
    
Sorry -- I think I should have said query. Without the query, it doesn't work. –  Scott Simpson Jun 28 '13 at 20:41
    
Where exactly are you trying to use this? –  s_ha_dum Jun 28 '13 at 20:53
    
mysite.com/blog. There is a template page set for the 'blog' page. It is named 'blog.php'. Under settings/reading posts page is set to 'blog'. –  Scott Simpson Jun 28 '13 at 21:05
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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