I have an events page which I would like to add pagination to. However, with it being a custom post type I am finding it rather difficult. I have managed to get pagination working for my news page, but I cannot get the same result for the events page. Here is my code for the events page
<?php
get_header();
get_sidebar('left');
?>
<article class="content-main events" role="main" id="post-<?php the_ID() ?>">
<?php include 'breadcrumbs.php'; ?>
<?php query_posts(array('posts_per_page'=>'2')); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="news-post">
<h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php the_excerpt() ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<!--Pagination-->
<?php echo paginate_links( $args ) ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</article><!-- //.content-main -->
<?php
get_footer();
If I try to change this
<?php query_posts(array('posts_per_page'=>'2')); ?>
to this
<?php query_posts(array('category_name'=>'events','posts_per_page'=>'2')); ?>
This doesnt work either. However, if I remove the line altogether, it shows up the news post types. I am stumped!
Pagination for custom post types should work the same as normal posts.
If you take a look at the default theme TwentyEleven you can see how they do it.
Basically they use next_posts_link() and previous_posts_link() functions.
You can look at that in functions.php > twentytwelve_content_nav();
Cheers,
you need to add global $paged and then in your array that is being passed to WP_Query you need to add 'paged' => $paged
">
<?php
global $paged;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array('post_type' => 'yourcustomepost','posts_per_page' => 2, 'paged' => $paged ) ); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="news-post">
<h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php the_excerpt() ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<!--Pagination-->
<?php echo paginate_links( $args ) ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</article><!-- //.content-main -->
<?php
just replace below function in functions.php file
note: in backend setting->reading->post per page : set this value to one
function twentyeleven_content_nav( $html_id )
{
global $wpdb;
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="<?php echo esc_attr( $html_id ); ?>">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
<div class="nav-previous">
<?php next_posts_link( __( ' Previous', 'twentyeleven' ) ); ?>
<?php
$count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_posts where post_type='post' and post_content<>''" );
for($j=1;$j<=$count;$j++)
{
echo "<a href='?paged=$j'> $j < </a>";
}
?>
<?php previous_posts_link( __( 'Next', 'twentyeleven' ) ); ?>
</div>
</nav><!-- #nav-above -->
<?php endif;
}