Skip to content Skip to sidebar Skip to footer

Wordpress Custom Post Type Index Pageination

I have created a custom post type for my theme called 'Projects'. On my projects page i am currently displaying all the projects on the one page, using the following code.

Solution 1:

Try this as per the codex:

Adding the "paged" parameter to a query

If WP_Query is altering the main loop and the "paged" parameter is not set you'll need to add it with get_query_var(). This is so WordPress knows exactly what page it's on.

For example, if your query looks like this (without the "paged" parameter):

<?php$the_query = new WP_Query( 'posts_per_page=3' ); ?>

you add the parameter like this:

<?php$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$the_query = new WP_Query( 'posts_per_page=3&paged=' . $paged ); 
?>

The next example is exactly the same as above but with the parameters in an array:

<?php$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 3,
  'paged'          => $paged
);

$the_query = new WP_Query( $args ); 
?>

So in your specific case it would be something like:

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

                echo'<div class="project p-project" data-filter="">';
                echo'<a href="'.get_permalink( $post->ID).'">';  
                the_post_thumbnail();
                echo'</a>'; 
                echo'</div>';

                endwhile;

            ?>

After adding in your pagination, it's also likely a good idea to reset the loop, so as to return WP to the physical page(rather than your list of archives). You can do this with:

<?php wp_reset_postdata(); ?>

Solution 2:

Hi Check below code for Pagination.

<?phpglobal$wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type' => 'bw_projects', 'posts_per_page' => 5, 'paged' => $paged);
$wp_query = new WP_Query($args); ?><?phpif ( $wp_query->have_posts() ) : ?><?phpwhile ( $wp_query->have_posts() ) : $wp_query->the_post(); ?><h2><?php the_title(); ?></h2><?phpendwhile; ?><divclass="pagination"><?php previous_posts_link( 'New projects;' ); ?><?php next_posts_link('Old projects;') ?></div><?php wp_reset_postdata(); ?><?phpelse:  ?><p><?php _e( 'No results' ); ?></p><?phpendif; ?><?php wp_reset_query(); ?>

Post a Comment for "Wordpress Custom Post Type Index Pageination"