简体   繁体   中英

How to create pagination on WordPress posts In custom page or template

I use this method

$args = array(
    'posts_per_page' => 4,
    'offset' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true);

$recent_postss = wp_get_recent_posts($args, ARRAY_A);
?> 

foreach ($recent_postss as $posts) { } 

Now I'd like to create a pagination for this. I've tried lots but nothing works for me. How should I modify this code to create pagination?

Try this:

$args = array(
    'posts_per_page' => 4,
    'offset' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged'    => (get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1),
    'suppress_filters' => true);

Sample code for Pagination Links:

the_posts_pagination( array(
                        'prev_text'          => __( 'Previous page', 'custom' ),
                        'next_text'          => __( 'Next page', 'custom' ),
                        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'P\
age', 'custom' ) . ' </span>',
                    ) );

Try to this it is working for custom post

$args = array(
    'posts_per_page' => 4,
    'offset' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged'    => (get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1),
    'suppress_filters' => true);

For pagination link

$argsp = array(
  'base'               => '%_%',
  'format'             => '?paged=%#%',
  'total'              => 5,
  'current'            => 2,
  'show_all'           => false,
  'end_size'           => 1,
  'mid_size'           => 2,
  'prev_next'          => true,
  'prev_text'          => __('« Previous'),
  'next_text'          => __('Next »'),
  'type'               => 'plain',
  'add_args'           => false,
  'add_fragment'       => '',
  'before_page_number' => '',
  'after_page_number'  => ''
);


echo paginate_links( $argsp );

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