简体   繁体   中英

Add pagination in a Wordpress Latest Posts widget

In a Wordpress Latest Posts widget I want to add pagination

 public function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', $instance['title'] ); $count = $instance['count']; $category = $instance['category']; echo $before_widget; $output = ''; if ( $title ) echo $before_title . $title . $after_title; global $post; if ( isset( $category ) && $category != '' ) { $args = array( 'category_name' => $category, 'posts_per_page' => $count, ); } else { $args = array( 'posts_per_page' => $count, ); } $posts = get_posts( $args ); if(count($posts)>0){ $output .='<div class="sp-latest-posts-widget latest-posts">'; foreach ($posts as $post): setup_postdata($post); $output .='<div class="media">'; if(has_post_thumbnail()): $output .='<div class="pull-left">'; $output .='<a href="'.get_permalink().'">'.get_the_post_thumbnail($post->ID, 'xs-thumb', array('class' => 'img-responsive')).'</a>'; $output .='</div>'; endif; $output .='<div class="media-body">'; $output .= '<h3 class="entry-title"><a href="'.get_permalink().'">'. get_the_title() .'</a></h3>'; $output .= '<div class="entry-meta small"><span class="st-lp-time">'. get_the_time() . '</span> <span clss="st-lp-date">' . get_the_date('d M Y') . '</span></div>'; $output .='</div>'; $output .='</div>'; endforeach; wp_reset_query(); $output .='</div>'; } echo $output; echo $after_widget; }
I tried with pagination_nav(); after adding code in functions.php and I used WP-PageNavi, but without success. How to add numbered pagination in this case? Thanks in advance!

Use below code for numbered pagination,

<?php the_posts_pagination( array(
      'mid_size'  => 1,
      'prev_text' => __( 'Prev', 'textdomain' ),
      'next_text' => __( 'Next', 'textdomain' ),
      'screen_reader_text' => ' ',
    ) );

     } ?>

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