Creating a simple pagination for custom post type templates

posts_nav_link() won’t work here

Useful for providing “paged” navigation of index, category and archive pages.

You will have to use the more generic next_posts_link() and previous_posts_link(). Just make sure to set the $max_pages parameter for next_posts_links() otherwise it will not work, and keep in mind, if by change, you are using this page as a static front page, then you should use page, not paged

EDIT 1

Use the above mentioned links as follows

next_posts_link( 'Older Entries', $loop->max_num_pages );
previous_posts_link( 'Newer Entries' );

EDIT 2

I think there is a slight misunderstanding. Try the following:

  • In your custom post type arguments where you register your custom post type, add the following, 'has_archive' => true. Your function should look like this

    function custom_post_news() {
        register_post_type( 'news',
            array('labels' => array(
                'name' => __('News', 'post type general name'), /* This is the Title of the Group */
                'singular_name' => __('News', 'post type singular name'), /* This is the individual type */
                'add_new' => __('Add New', 'custom post type item'), /* The add new menu item */
                'add_new_item' => __('Add New'), /* Add New Display Title */
                'edit' => __( 'Edit' ), /* Edit Dialog */
                'edit_item' => __('Edit'), /* Edit Display Title */
                'new_item' => __('New '), /* New Display Title */
                'view_item' => __('View'), /* View Display Title */
                'search_items' => __('Search news'), /* Search Custom Type Title */
                'not_found' =>  __('Nothing found in the Database.'), /* This displays if there are no entries yet */
                'not_found_in_trash' => __('Nothing found in Trash'), /* This displays if there is nothing in the trash */
                'parent_item_colon' => ''
            ), /* end of arrays */
                'description' => __( 'This is the example custom post type' ), /* Custom Type Description */
                'public' => true,
                'publicly_queryable' => true,
                'exclude_from_search' => false,
                'show_ui' => true,
                'query_var' => true,
                'menu_position' => 2, /* this is what order you want it to appear in on the left hand side menu */
                'capability_type' => 'post',
                'hierarchical' => false,
                'rewrite' => array('slug' => 'news', 'with_front' => true ),
                'has_archive' => true,
                /* the next one is important, it tells what's enabled in the post editor */
                'supports' => array( 'title', 'editor', 'thumbnail')
            )
        );
    
    }
    
  • Flush your permalinks after this addition and then visit the homepage

  • Create an archive-news.php. It should look like this

    <?php
        while ( have_posts() ) : the_post();
    ?>
    
    // Loop
    
    <?php endwhile; ?>
    
    <div id="pagination" class="clearfix">
        <?php posts_nav_link(); ?>
    </div>
    
  • Next, in your functions.php, use pre_get_posts to alter the main query as needed.

    add_action( 'pre_get_posts', function ( $q ) {
    
        if( !is_admin() && $q->is_main_query() && $q->is_post_type_archive( 'news' ) ) {
    
            $q->set( 'posts_per_page', 2 );
    
        }
    
    });
    
  • Delete the page that you have created in the back end. Everything should work perfectly now

Leave a Comment