简体   繁体   中英

How to add pagination in wordpress when using this code?

I'm using this code to show post loop on my homepage.

<?php 

    if ( have_posts() ) : while ( have_posts() ) : the_post();

        get_template_part( 'content', get_post_format() );

    endwhile; 
    endif; 

?>

And my content.php contain this code:

 <div class='post-outer'> <h2 class='entry-title'> <a href='<?php echo get_permalink(); ?>' id='pt'><?php the_title(); ?></a> </h2> <div class='pimg'> <img alt='featured image' id='fimg' src='<?php the_post_thumbnail_url(); ?>'/> </div> <div class='post-inner'> <?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 25 ) ); ?> <a href='<?php echo get_permalink(); ?>'>[Continue Reading]</a> </div> <div class='clear'></div> </div> 

I'm trying to add pagination in this code by showing only 5 post at a time in loop and adding next page option in it.

if you want with previous and next pages try this:

the_posts_pagination( array(
    'mid_size'  => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) );

or if you want without previous and next pages:

the_posts_pagination( array( 'mid_size'  => 2 ) );

after endwhile and endif .

and for setting post per page, go to Setting > Reading and set number in 'Blog pages show at most' field.

and here the complete pagination reference: Pagination

hope to be useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM