Support » Developing with WordPress » Adding Pagination

  • jtourns



    Looking to add pagination to the blog page on a site and I can’t seem to match the code with any examples in the codex. Any ideas?

    // Template Name: Blog Template
      <?php $loop = new WP_Query( array(
        'post_type' => 'post',
        'tax_query' => array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => 'blog',
        if( $loop->have_posts() ) :
          echo '<div class="container">';
            while( $loop->have_posts() ) : $loop->the_post();
            $image = get_field('featured_image');
              echo '<div class="post_container" data-aos="fade-up" data-aos-offset="0" data-aos-duration="1000">';
                echo '<div class="row justify-content-center align-items-center">';
                  echo '<div class="col col_3_12 post_image">';
                    echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
                  echo '</div>';
                  echo '<div class="col col_9_12 post_content">';
                  echo '<h4><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h4>';
                  echo '<p>' . wp_trim_words( get_the_content(), 85, '... <a href="' . get_the_permalink() . '">Read more</a></p>' );
                  echo '<p class="date">' . get_the_date() . '</p>';
                  echo '</div>';
                echo '</div>';
              echo '</div>';
        echo '</div>';
    <?php get_footer(); ?>


Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    You can use paginate_links() to generate pagination links for custom queries. You do need to set the “paged” or “offset” argument for the new WP_Query object or else you will always get page 1 content.

    IMO a better option is to customize the primary query through the “pre_get_posts” action. You can reconfigure any posts query into any other posts query you like this way. Then the usual pagination template tags will work automatically. Additionally, you save an extra query from being run.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.