Skip to content

Instantly share code, notes, and snippets.

@davidchase
Created March 20, 2013 13:42
Show Gist options
  • Save davidchase/5204713 to your computer and use it in GitHub Desktop.
Save davidchase/5204713 to your computer and use it in GitHub Desktop.
pagination inside of a function or shortcode
<?php
function paginationFunc() {
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'name_of_post_type', // post type custom or native
'orderby' => 'date', // how to order
'order' => 'DESC', // order ASC or DESC
'paged' => $paged,
'posts_per_page' => 2) //pages to show
);
$data = '<div>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$data .= '<div>'.get_the_date('m.d.Y').' &mdash; '.get_the_title().'</div>'; // use get in front of functions to store inside variables
$data .= wpautop(get_the_content()).'</div>';
endwhile;
$data .= '</div>';
$data .= '<nav>';
$data .= '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
$data .= '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
$data .= "</nav>";
wp_reset_postdata(); // Rest Data
return $data; // return the posts and pagination
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment