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.
<?php
$args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 18 );
$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;
?>
I am using archive-projects.php which is using pagination much the same as posts. Code as follows..
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/post/contentp', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/post/contentp', 'none' );
endif;
?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
How do i go about creating a paginated projects home page instead of just displaying all available projects. Would i just add to my existing array (example 1) or is there a specific template i should be using for example (home-projects.php) ?
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(); ?>
Hi Check below code for Pagination.
<?php
global $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); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<div class="pagination">
<?php previous_posts_link( 'New projects;' ); ?>
<?php next_posts_link('Old projects;') ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'No results' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>