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) ?
Hi Check below code for Pagination.
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):
you add the parameter like this:
The next example is exactly the same as above but with the parameters in an array:
So in your specific case it would be something like:
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: