Skip to content

Instantly share code, notes, and snippets.

@alwerner
Last active December 1, 2015 22:40
Show Gist options
  • Save alwerner/6ebc742279a225ca0a91 to your computer and use it in GitHub Desktop.
Save alwerner/6ebc742279a225ca0a91 to your computer and use it in GitHub Desktop.
for "news" custom post type
<?php
// Define custom query parameters
$custom_query_args = array(
'post_type' => 'news',
'orderby' => 'asc',
'paged' => $paged,
'posts_per_page' => 2
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<?php get_template_part( 'partials/list' , 'news' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<div class="nav-previous"><?php next_posts_link( 'Older', $custom_query->max_num_pages ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer' ); ?></div>
<?php
$wp_query = null;
$wp_query = $temp_query; // Reset
?>
<?php else : ?>
<header class="page-header">
<h1 class="page-title"><?php _e( 'Nothing Found' ); ?></h1>
</header>
<div class="page-content">
<?php if ( is_search() ) : ?>
<p><?php _e( 'Sorry, but nothing matched your search terms. Please try again with different keywords.'); ?></p>
<?php get_search_form(); ?>
<?php else : ?>
<p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.'); ?></p>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment