WordPress create pagination for custom post types

I have been trying to figure out how to break my custom post type categories into pages but im having trouble with it.

I am looking to make the archives break into new pages every 6 posts, if someone could show me how I can make this work that would be great.

Read More

This is the current layout of my archives:

<?php

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * News Archive Template
 */


 global $wp_query;
  $wp_query = new WP_Query( array ('posts_per_page' => 6, 'post_type' => 'news', 'post_status' => array('publish'), 
   'tax_query' => array( 
        array(
            'taxonomy' => 'news_category',
            'field' => 'slug', 
            'terms' => array( $wp_query->query['news_category']),
            'include_children' => true,
            'operator' => 'IN'
        ),
    )));

get_header(); ?>

<div id="content-archive" >

    <?php if( have_posts() ) : ?>

        <?php while( have_posts() ) : the_post(); ?>

            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <?php if( is_single() ): ?>
                    <h1 class="entry-title post-title"><?php the_title(); ?></h1>
                <?php else: ?>
                    <h2 class="entry-title post-title"><a href="<?php the_title(); ?></a></h2>
                <?php endif; ?>

                <div class="post-meta">
                <?php       
                printf( __( '<span class="%1$s">Posted on </span>%2$s<span class="%3$s"> by </span>%4$s', 'responsive' ),
                'meta-prep meta-prep-author posted',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="timestamp updated" datetime="%3$s">%4$s</time></a>',
                         esc_url( get_permalink() ),
                         esc_attr( get_the_title() ),
                         esc_html( get_the_date('c')),
                         esc_html( get_the_date() )
                ),
                'byline',
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                         get_author_posts_url( get_the_author_meta( 'ID' ) ),
                         sprintf( esc_attr__( 'View all posts by %s', 'responsive' ), get_the_author() ),
                         esc_attr( get_the_author() )
                )
            ); 
            ?>

                </div></div><!-- end of .post-meta -->
                <!-- end of .post-entry -->
                </div><!-- end of #post-<?php the_ID(); ?> -->

        <?php
        endwhile;
    else :
        echo "No posts here";

    endif;
    ?>
</div>
<!-- end of #content-archive -->

<?php get_footer(); ?>

Related posts

Leave a Reply

2 comments

  1. You need to add the “paged” parameter in the WP_Query().

    And add the paginate_links() for the navigation,

    Try this,

    // Exit if accessed directly
    if( !defined( 'ABSPATH' ) ) {
        exit;
    }
    
    /**
     * News Archive Template
     */
    
    
     global $wp_query;
     $paged = (get_query_var('page')) ? get_query_var('page') : 1;
      $wp_query = new WP_Query( array ('posts_per_page' => 6, 'post_type' => 'news', 'post_status' => array('publish'),
        'paged' => $paged,
       'tax_query' => array( 
            array(
                'taxonomy' => 'news_category',
                'field' => 'slug', 
                'terms' => array( $wp_query->query['news_category']),
                'include_children' => true,
                'operator' => 'IN'
            ),
        )));
    
    get_header(); ?>
    
    <div id="content-archive" >
    
        <?php if( have_posts() ) : ?>
    
            <?php while( have_posts() ) : the_post(); ?>
    
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
                    <?php if( is_single() ): ?>
                        <h1 class="entry-title post-title"><?php the_title(); ?></h1>
                    <?php else: ?>
                        <h2 class="entry-title post-title"><a href="<?php the_title(); ?></a></h2>
                    <?php endif; ?>
    
                    <div class="post-meta">
                    <?php       
                    printf( __( '<span class="%1$s">Posted on </span>%2$s<span class="%3$s"> by </span>%4$s', 'responsive' ),
                    'meta-prep meta-prep-author posted',
                    sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="timestamp updated" datetime="%3$s">%4$s</time></a>',
                             esc_url( get_permalink() ),
                             esc_attr( get_the_title() ),
                             esc_html( get_the_date('c')),
                             esc_html( get_the_date() )
                    ),
                    'byline',
                    sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                             get_author_posts_url( get_the_author_meta( 'ID' ) ),
                             sprintf( esc_attr__( 'View all posts by %s', 'responsive' ), get_the_author() ),
                             esc_attr( get_the_author() )
                    )
                ); 
                ?>
    
                    </div></div><!-- end of .post-meta -->
                    <!-- end of .post-entry -->
                    </div><!-- end of #post-<?php the_ID(); ?> -->
    
            <?php
            endwhile;
        else :
            echo "No posts here";
    
        endif;
        echo paginate_links();
        ?>
    </div>
    <!-- end of #content-archive -->
    
    <?php get_footer(); ?>
    
  2. Why don’t you use the default theme archive page? All you need to do to make it custom post type specific is copy the archive.php and rename it to archive-{posttype}.php

    Also – if you need anything specific to display on that post-type (like sidebars, menus, etc…) you can just edit that one single file.

    If you have a normal working pagination on your default archive page – this will work, and you can set the number of displayed post in your WP global admin options.

    If you don’t have a navigation implemented, I suggest using WP-PageNavi plugin.