【问题标题】:Add pagination in a Wordpress Latest Posts widget在 Wordpress 最新帖子小部件中添加分页
【发布时间】:2018-05-22 11:30:46
【问题描述】:

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

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    使用下面的代码进行编号分页,

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

    【讨论】:

    • 谢谢baiju jha的回答,但我的问题是我无法显示分页,无论是数字还是其他。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多