dimanche 1 février 2015

Creating a simple pagination for custom post type templates


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?





Aucun commentaire:

Enregistrer un commentaire