ayumee
9/22/2017 - 6:19 PM

Convert navigation to bootstrap style

function the_posts_pagination_bootstrap( $args = array() ) {
	$navigation = '';
	
	// Show navigation, if more than 2 pages
	if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
		$args = wp_parse_args( $args, array(
			'mid_size'           => 1,
			'prev_text'          => _x( 'Previous', 'previous post' ),
			'next_text'          => _x( 'Next', 'next post' ),
			'screen_reader_text' => __( 'Posts navigation' ),
		) );
		
		// type require to be list
		$args['type'] = 'list';

		// create pagination with wordpress default style
		$links = paginate_links( $args );
		
		if ( $links ) {

    // modify code with bootstrap style
    // maybe there is much more easy way to write
			$links = preg_replace(
				'#<ul class=\'page-numbers\'>#',
				'<ul class=\'pagination pagination-lg justify-content-center\'>',
				$links);

			$links = preg_replace("/Previous/", "<i class='fa fa-angle-left'></i> Previous", $links);

			$links = preg_replace("/Next/", "Next <i class='fa fa-angle-right'></i>", $links);

			$links = preg_replace("/<li>/", "<li class='page-item'>", $links);

			$links = preg_replace("/page-numbers/", "page-link", $links);

			$links = preg_replace("/<li class='page-item'><span class='page-link current/", "<li class='page-item active'><span class='page-link", $links);

			$template = '<nav aria-label="navigation">%1$s</nav>';
			$navigation = sprintf($template, $links);
		}
	}

	echo $navigation;
}