0

I have custom post post type call 'news' and I am struggling to get it to paginate correctly. I'm looking for a simple prev and next links, not worried about page numbers in the middle.

In page-news.php this is my code:

<?php
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array('post_type' => 'news', 'posts_per_page' => 2, 'paged' => $paged);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>

// Loop

<?php endwhile; wp_reset_postdata(); ?>

<div id="pagination" class="clearfix">
    <?php posts_nav_link(); ?>
</div>

The above code is showing 2 posts, but no pagination links

This is my custom post type code:

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 ),
            /* the next one is important, it tells what's enabled in the post editor */
            'supports' => array( 'title', 'editor', 'thumbnail')
        )
    );

}


// REGISTER TAXOMONIES

add_action( 'init', 'custom_post_news');
register_taxonomy( 'custom_news',
    array('news'), /* if you change the name of register_post_type( 'movies', then you have to change this */
    array('hierarchical' => true,
        'labels' => array(
            'name' => __( 'News Categories' ), /* name of the custom taxonomy */
            'singular_name' => __( 'news Category' ), /* single taxonomy name */
            'search_items' =>  __( 'Search news Categories' ), /* search title for taxomony */
            'all_items' => __( 'All news Categories' ), /* all title for taxonomies */
            'parent_item' => __( 'Parent news Category' ), /* parent title for taxonomy */
            'parent_item_colon' => __( 'Parent news Category:' ), /* parent taxonomy title */
            'edit_item' => __( 'Edit news Category' ), /* edit custom taxonomy title */
            'update_item' => __( 'Update news Category' ), /* update title for taxonomy */
            'add_new_item' => __( 'Add New news item' ), /* add new title for taxonomy */
            'new_item_name' => __( 'New Custom news' ) /* name title for taxonomy */
        ),
        'show_ui' => true,
        'query_var' => true,
    )
);

This is a custom theme (started from scratch) so there's nothing in my functions.php referring to a pagination. I've scoured the Internet for answers, but I get a different solution and nothing seems to work.

I also don't have an archive-news.php as I'm not sure if this needs to be implemented or not?

4

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

  • Sorry, I see you have just commented just a I figured it out so I deleted them. Anyway, thanks for the updates. It works in terms of showing the 'Older entries' link, but it links back to the index.php page – Jack Barham Feb 1 '15 at 18:51
  • That has something to do with your rewrite rules. Did you flush your permalinks – Pieter Goosen Feb 1 '15 at 18:52
  • Opps, thought that would format – Jack Barham Feb 1 '15 at 18:55
  • That code (second code block in the question) is already in my functions.php - I've done another flush by re-saving the permalinks settings page and still not working. – Jack Barham Feb 1 '15 at 19:13
  • Sure, I deleted that comment now. I've separated the endwhile and placed it after the loop, then added my next and prev post_links then closed it with wp_reset_postdata(); flushed the cache and still hitting the index.php – Jack Barham Feb 1 '15 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.