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?

Related posts

2 comments

  1. 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 );
    
  2. 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>',
                        ) );
    

Comments are closed.