Wordpress paginate custom post type
Solution 1
Have a look at next page link page, the example here will help. codex.wordpress.org/Template_Tags/next_posts_link
<?php next_posts_link('Older Entries »', 0); ?>
Wordpress codex example.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after our query
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Solution 2
Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news= new WP_Query(array(
'post_type'=>'post',
'posts_per_page' => 3,
'paged' => $paged,
));
if($news->have_posts()) :
while($news->have_posts()) : $news->the_post();
the_title();
endwhile;
$total_pages = $news->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
scferg5
Updated on June 04, 2022Comments
-
scferg5 almost 2 years
I have a page called News (using page template
page-newslist.php
), which is supposed to display posts from the custom post type also named News. I realize that having both with the same name causes issues, so when registering the custom post type, I have a rewrite to differentiate it from the page:'rewrite' => array('slug' => 'news-article', 'with_front' => true),
I can get the query working and displaying the posts properly, but after all of the articles and posts I've read, I cannot get the pagination to work. Nothing ever shows up for the pagination.
After no success with using the page template query, I tried the
archive-news.php
method, where it would automatically display the posts from the custom post type. The pagination does work there. The downside of using this method is that there isn't a 'physical' page to tie it to (which would also have custom fields, ability to be nicely added (not hard-coded) into menus, etc.)Here is the stripped-down code registering the custom post type:
register_post_type('news', array( 'label' => 'News', 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'news-article', 'with_front' => true), 'query_var' => true, 'has_archive' => true, ));
And then the code for the page template:
$paged = 1; if ( get_query_var('paged') ) $paged = get_query_var('paged'); if ( get_query_var('page') ) $paged = get_query_var('page'); $args = array( 'post_type' => 'news', 'post_status' => 'publish', 'posts_per_page' => 1, 'paged' => $paged ); $my_query = null; $my_query = new WP_Query($args); if($my_query->have_posts()): while ($my_query->have_posts()) : $my_query->the_post(); ... endwhile; endif; wp_reset_query(); // Attempt method 1 posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); // Attempt method 2 previous_posts_link('« Newer'); next_posts_link('Older »');
Any ideas what's wrong?
-
scferg5 about 10 yearsIt's also worth nothing that after I changed
$my_query
to$wp_query
, it seemed to fix some of the issues with other pagination methods not working (such as the WP-PageNavi plugin). -
Chonchol Mahmud over 7 yearsIts showing pagination but when i click one second pagination page then the content remain same.How to solve this?
-
Purvik Dhorajiya over 7 yearsCan you please tell me which permalink structure do you have use?