-1

I have the following code:-

<!-- News Archive -->
<div id="news-archive">
    <div class="row">
        <div class="col-md-12"> 
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
          'post_type'       => 'post',
          'orderby'         => 'title',
          'order'           => 'ASC',
          'cat'             => '8',
          'offset'          => 2,
          'posts_per_page'  => 6,
          'paged'           => $paged
        );
        $loop = new WP_Query($args);
        $post_counter = 1;
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-xs-12 col-sm-4 col-md-4">
                <div id="blog-<?php echo $post_counter; ?>" class="blog-wrapper">
                    <?php if(get_the_post_thumbnail()) {
                        the_post_thumbnail();
                    } else {
                    $category = "/wp-content/themes/irongate/assets/img/" . get_field('group_category') . '-default.jpg'; ?>
                        <img src="<?php echo $category; ?>" />      
                    <?php } ?>
                    <span class="news-archive-date"><?php echo get_the_date('d M Y'); ?></span>
                    <p class="news-archive-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                    <span id="<?php echo $case_category; ?>-logo" class="<?php echo get_field('group_category') == 'inone' ? 'inone-catergory' : 'news-archive-catergory';?>"></span>
                    <a href="<?php the_permalink(); ?>">
                        <div class="news-overlay blog-overlay">
                            <span class="news-overlay-excerpt"><?php echo substr(get_the_excerpt(), 0,240); ?>...</span>
                            <span id="careers-overlay-more" class="btn-white">Read more</span>
                        </div>
                    </a>                
                </div>
            </div>
            <?php if($post_counter % 3 == 0) {echo '<div class="clearfix"></div>';} 
        $post_counter++;  
        endwhile; ?><!-- News Archive -->
        <div class="navigation">
            <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
            <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
        </div>
        <?php wp_reset_postdata(); ?>
        </div>
    </div>
</div>

The display of the posts is showing correctly as 6 blog posts and the 'More...' button appears but when clicked it just reloads the same content.

How can I add pagination to my blog posts? i.e. clicking next... will show posts 9-14 instead of 2-8 (as I'm using an offset of 2).

Thanks in advance!

  • 2
    offset breaks pagination, so you manually need to recalculate pagination. Also, why are you running a custom query\ – Pieter Goosen Jul 19 '16 at 10:47
  • Before I forget, next_posts_link() are by default set to the main query, and not your custom query – Pieter Goosen Jul 19 '16 at 10:53
  • ok, so what is the correct function? I've removed the offset as well @PieterGoosen – nsilva Jul 19 '16 at 10:55
  • 1
    It depends, if this is suppose to be the main query, you need to use the default loop and pre_get_posts to alter the main query according to your needs – Pieter Goosen Jul 19 '16 at 10:58
  • 1
    I wonder, this being a normal page, you can try to use my PreGetPostsForPages Class to alter the main query and inject your custom posts in there – Pieter Goosen Jul 19 '16 at 15:57
4
+50

You can calculate offset via paged and posts_per_page. E.g:

$per_page = 6;
$paged    = get_query_var('paged') ? : 1;
$offset   = (1 === $paged) ? 0 : (($paged - 1) * $per_page) + (($paged - 1) * 2);

$args = array(
  'order' => 'ASC',
  'paged' => $paged,
  'offset' => $offset,
  'orderby' => 'ID',
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => $per_page,
  'ignore_sticky_posts' => 1
);

$query = new WP_Query($args);

while ( $query->have_posts() ) : $query->the_post();
  echo get_the_title() . '<br>';
endwhile;

previous_posts_link('&laquo; Previous', $query->max_num_pages);
if ($paged > 1) echo ' | ';
next_posts_link('More &raquo;', $query->max_num_pages);

echo '<br> Showing ' . $offset . '-' . ($offset + 6) . ' of ' . $query->found_posts . ' posts.';

wp_reset_postdata();

Note that the loop start at index 0, so if we ignore sticky posts and only display 6 posts per page, the result on page 2 should be 8-14 instead 9-14 as you expected.

  • 1
    Ps: We can set the default value with get_query_var('paged', 1 ). Wonder about the default 0 offset here, it looks like we would then miss out items #7 and #8 in that case. I think it's not necessary to set the paged in WP_Query when we use offset? The $query->found_posts might be relevant here instead of count( $query->posts). – birgire Jul 25 '16 at 15:30
  • 1
    @birgire I thought that OP want to skip two posts then I added 2 to the offset. ;-) Didn't know about the $default param in get_query_var(). Thank you for showing me that! – MinhTri Jul 25 '16 at 15:40
1

If you are useing next_posts_link() && previous_posts_link() on your custom Query then you have to pass $max_pages parameter. like ->

previous_posts_link( '&laquo; Previous', $loop->max_num_pages );
next_posts_link( '&laquo; Previous', $loop->max_num_pages );

Or if you want to use just previous_posts_link( '« Previous'); without useing $max_pages parameter then you have to use default query instead custom query. And it's better to used default one. see more details When to use WP_query(), query_posts() and pre_get_posts

0

Or you can use paginate_links:

//First get a count of how many pages you'll be producing:

$cat_id = 8;
$per_page = 6;

$all_posts = get_term_by( 'id', $cat_id, 'category' );
$posts_count = $all_posts->count ;
$total_pages = intval( $posts_count / $per_page ) + 1;

$current_page = max(1, get_query_var( 'paged' ) );

echo '<div class="pagination">' ;

echo paginate_links(array(
    //several different ways to do the base variable in my observation/experience
    'base' => get_pagenum_link(1) . '%_%',
    'current' => $current_page,
    'total' => $total_pages,
    //if you like the double arrow (Euro quotes) format
    'prev_text' => '&laquo; Previous',
    'next_text' => 'Next &raquo;' 
) );

echo '</div>' ;

Some variations may be required depending on your permalinks style, and you can find a lot of interesting formatting for the links themselves based on available parameters and standard or customized output, but I think this should answer the basic question. See ttps://codex.wordpress.org/Function_Reference/paginate_links for other basic examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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