• Hi,

    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?

    <?php
    // Template Name: Blog Template
    get_header();
    ?>
    
      <?php $loop = new WP_Query( array(
        'post_type' => 'post',
        'tax_query' => array(
            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>';
    
            endwhile;
            wp_reset_query();
    
        echo '</div>';
    
      endif;
    
      ?>
    
    <?php get_footer(); ?>
    

    Thanks!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@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.
    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

Viewing 1 replies (of 1 total)
  • The topic ‘Adding Pagination’ is closed to new replies.