I'm a newbie with wordpress theme creation. I want to create a custom category.php file, I've implemented the loop and it will display the last 8 posts. I have some fake posts to test if all works well, but I'm not able to add a pagination to the category template I have created.

How I can implement the pagination?I'm thinking to use an infinite scrll using the wp rest api but any other solution will be appreciated. This is the code I'm using now, the previous link and the next are not displayed

<?php get_header(); ?>
<?php $current_cat = get_queried_object(); ?>
<div class="container mt-5 pt-5">
    <div class="row m-0">
        <div class="col-sm-12 col-md-12 col-lg-12 mt-2 mb-2">
            <p class="text-uppercase font-weight-bold mt-2 mb-2 section-title"><?php echo $current_cat->cat_name; ?></p>
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>        
        <div class="col-sm-12 col-md-3 col-lg-3 h-100 mt-2 mb-2">
            <img class="img-fluid w-100 h-100" src="<?php echo the_post_thumbnail_url(); ?>">
            <a class="h5 mt-2 mb-2" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <small class="text-muted d-block"><?php echo get_the_date('j F Y' ,$post->ID); ?></small>
    <?php echo next_posts_link(); // not working?>
    <?php echo previous_posts_link(); // not working?>
<?php endwhile; endif; ?>    

<?php get_footer(); ?>
  • Try moving them outside of the row div. – Jacob Peattie Jul 7 at 15:46
  • I've already tried, the two pagination function in the beginning was outside the loop and outside the row, but I had the same problem. – wpnewbie Jul 7 at 15:48
  • I can't see any obvious problem in the code, unless you just don't have more than one page's worth of posts. Are you using query_posts() or the pre_get_posts filter anywhere? – Jacob Peattie Jul 7 at 15:50
  • nope, I have 15 posts in that category and I only see eigth of them. No filter is applied. – wpnewbie Jul 7 at 16:09
  • is the code in category.php? also, there should be no echo with those functions developer.wordpress.org/reference/functions/next_posts_link – Michael Jul 8 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.