Created
January 13, 2016 12:37
-
-
Save Iamronan/91af599bc7273f613bea to your computer and use it in GitHub Desktop.
Custom Wordpress Loop With Pagination
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 | |
/* | |
Template Name: Test Template Page | |
*/ | |
?> | |
<?php get_header(); ?> | |
<!-- begin content --> | |
<div id="content"> | |
<!-- begin container --> | |
<div class="container"> | |
<!-- begin main --> | |
<div id="main" class="main-content-full"> | |
<?php | |
// set up or arguments for our custom query | |
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; | |
$query_args = array( | |
'post_type' => 'post', | |
'category_name' => 'tutorials', | |
'posts_per_page' => 2, | |
'paged' => $paged | |
); | |
// create a new instance of WP_Query | |
$the_query = new WP_Query( $query_args ); | |
?> | |
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?> | |
<article> | |
<h1><?php echo the_title(); ?></h1> | |
<div class="excerpt"> | |
<?php the_excerpt(); ?> | |
</div> | |
</article> | |
<?php endwhile; ?> | |
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?> | |
<nav class="prev-next-posts"> | |
<div class="prev-posts-link"> | |
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?> | |
</div> | |
<div class="next-posts-link"> | |
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?> | |
</div> | |
</nav> | |
<?php } ?> | |
<?php else: ?> | |
<article> | |
<h1>Sorry...</h1> | |
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> | |
</article> | |
<?php endif; ?> | |
</div> <!-- end #main --> | |
</div> | |
<!-- end container --> | |
</div> <!-- end #content --> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment