Skip to content

Instantly share code, notes, and snippets.

@jaclyntan
Created February 1, 2017 03:05
Show Gist options
  • Save jaclyntan/f0f979bdce9f6fa92f0bb87c6d011fe6 to your computer and use it in GitHub Desktop.
Save jaclyntan/f0f979bdce9f6fa92f0bb87c6d011fe6 to your computer and use it in GitHub Desktop.
Offset the WordPress loop
<?php
$number_of_posts_per_page = 9;
$initial_offset = 1;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
// Use paged if this is not on the front page
$number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
$off = $initial_offset + (($paged > 1) ? $number_of_posts_past : 0);
// WP_Query arguments
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => $number_of_posts_per_page,
'paged' => $paged,
'orderby' => 'date',
'offset' => $off
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
$counter = 0;
echo '<div class="row posts-row">'; //start the row
while ( $query->have_posts() ) {
$query->the_post();
$counter++;
if ($counter === 1) {
echo '<div class="col-sm-4 col">';//start the column
}
//display each post in a box
echo '<div class="blog-box">';
//display the title
the_title(sprintf('<h3><a href="%s" >', get_permalink()),'</a></h3>');
//get the post permalink
$permalink = get_permalink();
echo '<a href="' . $permalink . '">Read More <div class="arrow-right"></div></a>';
echo '</div>';//end the blog box and all of it's content
if ($counter === 3) {
$counter = 0; //reset counter
echo '</div>';//end the column
}
} // End of the loop
echo '</div>'; //close the row
//display pagination
echo '<div class="page-navigation">';
the_post_navigation();
echo '</div>';
}
else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment