Custom post type pagination issues
-
Hey,
I’ve done multiple attempts for this archive pagination to work, however I’ve been unsuccessful and can’t understand what’s the issue, it always loads to page not found… Any idea/suggestion would be appreciated, since I’ve been at this for the past 10h+…
The query with pagination:$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'news',
'posts_per_page' => 3,
'paged' => $paged
);
$news_query = new WP_Query($args);
if ($news_query->have_posts() ) : ?>
while ($news_query->have_posts() ) : $news_query->the_post();
get_template_part( 'template-parts/content-news-archive', get_post_format() );
endwhile; ?>
<?php
if ($news_query->max_num_pages > 1) :
?>
<div class="pagination">
<?php
$big = 999999999;
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $news_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'mid_size' => 3,
));
?>
</div>
<?php
endif;
?>
<?php else :?>
<p>No news items found</p>the registered CPT in functions.php
add_action( 'init', 'create_post_type_news' );
function create_post_type_news() {
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News Item' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-welcome-write-blog',
'supports' => array('title', 'editor', 'thumbnail'),
'taxonomies' => array('category'),
'rewrite' => array('slug' => 'news', 'with_front' => true),
)
);
}
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.