Display Custom Post Type Posts With Pagination - WordPress

<?php // Method One (While Loop) /* For Example Custom Post Type Name : moustapha Show Posts : 3 */ $page = (get_query_var('paged')) ? get_query_var('paged') : 1; $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $wp_query -> query('post_type=moustapha&showposts=3'.'&paged='.$paged); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <div class="post"> Do loop Here .... </div> <?php endwhile; ?> <div class="arch-pagenavi"> <?php if(function_exists('wp_pagenavi')) : ?> <?php wp_pagenavi() ?> <?php else : ?> <div class="pagenav"> <div class="alignleft"><?php previous_posts_link('Previous') ?></div> <div class="alignright"><?php next_posts_link('Next') ?></div> </div> <?php endif; ?> <div class="fixed"></div> </div> <?php $wp_query = null; $wp_query = $temp; ?> <?php // Method Two (Foreach) /* For Example Custom Post Type Name : moustapha Show Posts : 3 */ $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=3'.'&paged='.$paged); $myposts = get_posts('post_type=moustapha&orderby=post_date&order=DESC&showposts=3'.'&paged='.$paged); foreach($myposts as $post) : setup_postdata($post); ?> <div class="post"> Do loop Here .... </div> <?php endforeach; ?> <div class="arch-pagenavi"> <?php if(function_exists('wp_pagenavi')) : ?> <?php wp_pagenavi() ?> <?php else : ?> <div class="pagenav"> <div class="alignleft"><?php previous_posts_link('Previous') ?></div> <div class="alignright"><?php next_posts_link('Next') ?></div> </div> <?php endif; ?> <div class="fixed"></div> </div> <?php $wp_query = null; $wp_query = $temp; ?>
The Two Methods Works 100% With Any Custom Post Type .... ;)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.