Wordpress is not new for me but I am really stuck on this piece of code that I can not get to work properly.
I register a custom post type News and let the custom post type use the Wordpress categories
'taxonomies' => array('category')
So far so good. I can assign different categories to the custom post type News. But I want that people can see news from a specific category. So I want that to do that with the category.php page. The problem is that the pagination is not working. I get a page not found when I go to /page/2
I use this code on a category.php page.
// Get post ID
$post_type = get_post_type( $post->ID );
// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));
// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// WP_Query arguments
$args_news = array (
'post_type' => array( 'news' ),
'pagination' => true,
'posts_per_page' => '9',
'orderby' => 'date',
'paged' => $paged,
'cat' => $category_id,
);
// The Query
$query = new WP_Query( $args_news );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<div>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
</div>
<?php }
} else {
// no news found
}
?>
<div class="pagination">
<div class="previous-page"><?php previous_posts_link( 'Newer Posts' );?></div>
<div class="next-page"><?php next_posts_link( 'Older Posts', $query->max_num_pages ); ?></div>
</div>
<?php
// Reset postdata
wp_reset_postdata();