0

I can't add a pagination to divide my posts between pages and navigate between pages. Currently all 20 posts I have loads at once. I want to add a pagination to the bottom of my Page and view posts by 5.

I have a custom post type that I defined as Project. I have 2 pages. Homepage and Journal. Homepage features the feed of my Project post type and Journal features my regular blog posts. I want both of these pages to have pagination.

My current index.php which is my Homepage is as follows:

<?php get_header(); ?>
<?php get_footer(); ?>

<div class="portfolioBlock">    
<?php 
         $args = array('post_type' => 'project','posts_per_page' => 20 );
         $loop = new WP_Query( $args );

         if( $loop->have_posts() ):
            while( $loop->have_posts() ): $loop->the_post();            
            get_template_part( 'content', get_post_format() );
         endwhile;
         endif;
?>
</div>

My readings settings is as follows: Reading-Settings

I am curious if this is caused because of my custom post types, would appreciate any help or lead to help me add a pagination to the page.

When i tried the Googled solution paginate_links I didn't get any links. I also tried previous_posts_link() and that gives me the links and I can move between pages, but each page reloads the same 20 posts, without any diving it up.

Thanks a lot!

1
  • For static page pagination is a little bit different because the global variable $post behave differently. You may refer this and this post to see if it helps.
    – simongcc
    Apr 24, 2020 at 8:34

1 Answer 1

0

If you want to show only 5 posts you should set posts_per_page to 5. And by setting paged parameter to any number you want, you can query posts in a specific page. For example if you want to get posts 6 to 10 you should do:

$args = array(
    'post_type'      => 'project',
    'posts_per_page' => 5,
    'paged'          => 2 
);

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.