Created
March 20, 2013 13:42
-
-
Save davidchase/5204713 to your computer and use it in GitHub Desktop.
pagination inside of a function or shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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').' — '.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