Skip to content

Instantly share code, notes, and snippets.

@alexgraham
Last active August 29, 2015 14:10
Show Gist options
  • Save alexgraham/e775a8989985806e9672 to your computer and use it in GitHub Desktop.
Save alexgraham/e775a8989985806e9672 to your computer and use it in GitHub Desktop.
WordPress custom post type WP_Query on front_page.php with working pagination
<?php $paged = ( get_query_var('page') ) ? get_query_var('page') : 1; ?>
<?php $gallery_args = array(
'paged' => $paged,
'post_type' => 'gallery',
'posts_per_page' => 1
); ?>
<?php $gallery_query = new WP_Query( $gallery_args ); ?>
<?php // Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $gallery_query; ?>
<div>
<?php if( $gallery_query->have_posts() ) : ?>
<?php while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
<?php //loop content here ?>
<?php endwhile; ?>
<?php endif; ?>
<?php if ($gallery_query->max_num_pages > 1) : ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $gallery_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php $wp_query = NULL; ?>
<?php $wp_query = $temp_query; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment