1

I want to add pagination in single page of custom post type.

This is the code for single page (custom post type):

<?php
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array('post_type' => 'news', 'posts_per_page' => 1, '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 next_posts_link( 'Older Entries', $loop->max_num_pages );
previous_posts_link( 'Newer Entries' );//posts_nav_link(); ?>
</div>

////

Function page code ////


////

add_action( 'init', 'create_post_type' );
function create_post_type() {
    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')
        )
    );
}
0

The functions

next_posts_link

and

previous_posts_link

should be called before you reset the post data.

0

Reset query after pagination link Try this:

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

// Loop

<?php endwhile; ?>

<div id="pagination" class="clearfix">
    <?php next_posts_link( 'Older Entries', $loop->max_num_pages );
previous_posts_link( 'Newer Entries' );//posts_nav_link(); ?>
</div>

<?php wp_reset_query();  ?>
  • Not Working.. I've added same code – Sarjil Mar 7 '17 at 15:16
0

next_posts_link and previous_posts_link both check is_single and prevent output in that case. They are intended for archive pages and will not work on singular views.

0

Don't need to start with a new Query in archive.php loop

Only need to create a different loop file for the custom taxonomy and default loop post

See here for more details Filtering a custom post type by custom taxonomy in archive template

example I have taxonomy "service"

archive.php file


<?php 
get_header();

if (is_post_type_archive('service') || is_tax('services-category') || is_tax('services-tags')){
    get_template_part('my-loop-service'); // Get Loop for Taxonomy service
} else {
   get_template_part('my-loop-post'); // Default Loop for Post

}

    // Navigation
    echo '<div id="my-navigation">';
    the_posts_pagination( array(
        'mid_size' => 2,
        'prev_text' => __( 'Prev', 'text-domain' ),
        'next_text' => __( 'Next', 'text-domain' ),
    ));
    echo '</div>';

get_footer(); 
?>

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.