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?
Have a look at next page link page, the example here will help. codex.wordpress.org/Template_Tags/next_posts_link
WordPress codex example.