在Wordpress最新帖子小部件中添加分页

时间:2017-12-08 08:49:24

标签: php wordpress pagination

在Wordpress最新帖子小部件中,我想添加分页



	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;
	}
&#13;
&#13;
&#13; 我试过pagination_nav();在functions.php中添加代码后,我使用了WP-PageNavi,但没有成功。 在这种情况下如何添加编号分页? 提前致谢!

1 个答案:

答案 0 :(得分:0)

使用以下代码编号分页,

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

     } ?>