I’m submitting a quite handy function to apply WordPress Pagination in Bootstrap 4 Style. This function contains First/Last, Previous/Next, Current and total number of pages. You can use the code for pagination in your Bootstrap 4 powered WordPress theme. It works with Custom Post Types and hides First/Last, Previous/Next texts on small devices (viewport width < 768 px)
The article Bootstrap 3 Pagination In WordPress contains same technique if you wish to apply pagination in WordPress Theme having version 3 of Bootstrap.
WordPress Pagination in Bootstrap 4 Style
The function is given in code block below and later I have described important terms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | function fellowtuts_wpbs_pagination($pages = '', $range = 2) { $showitems = ($range * 2) + 1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages) $pages = 1; } if(1 != $pages) { echo '<nav aria-label="Page navigation" role="navigation">'; echo '<span class="sr-only">Page navigation</span>'; echo '<ul class="pagination justify-content-center ft-wpbs">'; echo '<li class="page-item disabled hidden-md-down d-none d-lg-block"><span class="page-link">Page '.$paged.' of '.$pages.'</span></li>'; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<li class="page-item"><a class="page-link" href="'.get_pagenum_link(1).'" aria-label="First Page">«<span class="hidden-sm-down d-none d-md-block"> First</span></a></li>'; if($paged > 1 && $showitems < $pages) echo '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($paged - 1).'" aria-label="Previous Page">‹<span class="hidden-sm-down d-none d-md-block"> Previous</span></a></li>'; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) echo ($paged == $i)? '<li class="page-item active"><span class="page-link"><span class="sr-only">Current Page </span>'.$i.'</span></li>' : '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($i).'"><span class="sr-only">Page </span>'.$i.'</a></li>'; } if ($paged < $pages && $showitems < $pages) echo '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($paged + 1).'" aria-label="Next Page"><span class="hidden-sm-down d-none d-md-block">Next </span>›</a></li>'; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<li class="page-item"><a class="page-link" href="'.get_pagenum_link($pages).'" aria-label="Last Page"><span class="hidden-sm-down d-none d-md-block">Last </span>»</a></li>'; echo '</ul>'; echo '</nav>'; //echo '<div class="pagination-info mb-5 text-center">[ <span class="text-muted">Page</span> '.$paged.' <span class="text-muted">of</span> '.$pages.' ]</div>'; } } |
I have made a little change here, If you want to let pagination number Page x of z
appear as text below list and not with pagination itself, comment out line #21 and un-comment line #43. Also occurrence of five hidden-*
classes here are to comply with alpha version of Bootstrap 4. You’re free to remove those if using beta or upper versions.
Also on extra small devices (viewport width < 576px), I have reduced padding and font-size in pagination. You can place the rule in theme CSS if the list breaks the container size. You should also play with Bootstrap classes used in function to let the pagination fit within content-area of your theme.
1 2 3 4 5 6 | @media screen and (max-width: 575px) { .ft-wpbs .page-link { font-size: .7rem; padding: .3rem .45rem; } } |
Also Read: A Bigger & Fixed Search Box With Dropdown In Bootstrap Navbar
Function call for Bootstrap WordPress Pagination
Now just call the function in your archive files to add Bootstrap style pagination.
1 2 3 4 5 6 7 | <?php if (function_exists("fellowtuts_wpbs_pagination")) { fellowtuts_wpbs_pagination(); //fellowtuts_wpbs_pagination($the_query->max_num_pages); } ?> |
If you’re using custom query (or Custom Post Types), you need to pass parameter during function call. Example provided in line number #5 in code above. The second parameter passed to function is range (Number of page links appear before and after the current page).
Now check pagination in your WordPress website with Bootstrap 4 theme. Here in the blog Fellow Tuts, we have utilizes WordPress Pagination in Bootstrap 4 Style with minor change.
How do I keep the first .li (says Page 1 of 2 ) from disappearing when the screen is smaller?
Hello @knoxhomesandrealty:disqus
Remove “hidden-md-down d-none d-lg-block” from line number 21. It will always display the Page X of Z text. Do let me know if you have any further concern.
On a static front page the active state is always 1 instead of the current page. How to fix this?
I know that pagination was not intended to be used on (static ) front pages.
@leonwakker:disqus
I believe that you’re using a custom query to display paginated posts on the front page. In that case, retrieve the $paged argument and pass it as the query argument. Like this below:
if ( get_query_var( ‘paged’ ) ) { $paged = get_query_var( ‘paged’ ); }
elseif ( get_query_var( ‘page’ ) ) { $paged = get_query_var( ‘page’ ); }
else { $paged = 1; }
$args=array(‘post_type’ => ‘POST_TYPE’,’ posts_per_page’ => 6, ‘paged’ => $paged);
$the_query = new WP_Query($args);
OR
query_posts($args);
It is referred in WordPress Codex under section Static Front Page.
If it’s not the question then provide me a bit of your code and explain the question so that I can help you.
https://uploads.disquscdn.com/images/4a494cc6c4e18e8cd3038786076d67c71d41d02b3eded3c68ecb3102af9d65ad.jpg
This is de code I used. Pagination is working but the active state of the current page is always set to page number 1 when going to the next page. (see image for illustration)
if ( get_query_var( ‘paged’ ) ) { $paged = get_query_var( ‘paged’ ); }
elseif ( get_query_var( ‘page’ ) ) { $paged = get_query_var( ‘page’ ); }
else { $paged = 1; }
$arg = (array(
‘post_type’ => $instance[ ‘selected_posttype’],
‘cat’ => $instance[ ‘selected_category’ ] ,
‘post_status’ => ‘publish’,
‘order’ => ‘ASC’,
‘posts_per_page’ => $instance[ ‘post_per_page’],
‘paged’ => $paged
));
$the_query = new WP_Query( $arg );
?>
have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// output
endwhile;
if (function_exists(“fellowtuts_wpbs_pagination”))
{
//fellowtuts_wpbs_pagination();
fellowtuts_wpbs_pagination($the_query->max_num_pages);
};
else :
// no posts found message goes here…
endif;
}
wp_reset_query();
@leonwakker:disqus
Would you try with replacing the line no #5 in the fellowtuts_wpbs_pagination() function with
if(empty($paged)) {
if ( get_query_var( ‘paged’ ) ) { $paged = get_query_var( ‘paged’ ); }
elseif ( get_query_var( ‘page’ ) ) { $paged = get_query_var( ‘page’ ); }
else { $paged = 1; }
}
And do let me know.
Yes, this is working perfectly! Thanks for your help!
You’re welcome. Do let us know if there are web/android apps development services required.
Something is going weird with this. For a page with 220 paginated posts at 40 posts_per_page, it should capping out at 6 paginated-page links – but it is giving me 22. All of them after 6 have no content. The same problem for other pages, where the problem is amplified according to post count.
Am I doing something wrong?
Here is my version of your function https://pastebin.com/j5xLH2JG and here is my template https://pastebin.com/sV2QGwvK (see circa line 128). Thanks.
@disqus_dNllO3GqNb:disqus
You’re calling wp_reset_postdata(); at line #154, before calling the pagination function.
Also, at line #162, you need to write it as:
fellowtuts_wpbs_pagination($the_query->max_num_pages);
Ah-hah. That works. Thank-you.
@disqus_dNllO3GqNb:disqus
I’m glad you did it.
PS.: I sent you a message at your Twitter account. Hope it’s fine and would get the reply.
Shouldn’t this be hidden if nopaging is true? I have commented-out posts_per_page and paged, though not fellowtuts_wpbs_pagination(); Whilst all posts are now showing on my page, hence no need for a paginator, the code is still pumping out a pagination stepper.
@disqus_dNllO3GqNb:disqus
I understand your concern. The nopaging parameter is by default set as false so I think you are using the standard loop with changing the parameter to true.
In this case, I would suggest you to create another function that calls this fellowtuts_wpbs_pagination() function only after checking the nopaging parameter from the WP_Query object. Interfering with the function that displays pagination is neither a good idea nor I wish to complicate the code for users feasibility.
How about this… ?
// Pagination
if ( false === $query->query_vars[‘nopaging’] ) {
fellowtuts_wpbs_pagination();
}
@disqus_dNllO3GqNb:disqus
It doesn’t need strict type comparison and I would prefer to write in a function in the theme’s functions.php file. It will keep me free from caring the name of WP_Query object like $the_query or $qry or $query.
function fellowtuts_apply_paging($nopaging, $mnp, $range = 2) {
if(!$nopaging)
fellowtuts_wpbs_pagination($mnp, $range);
}
And call this function like:
fellowtuts_apply_paging($the_query->nopaging, $the_query->max_num_pages, 4);
Seems a little complex for me. Going to stick with the conditional where I call the function. Thanks for this.
Not a problem,
Just use the condition like this:
if ( ! $query->nopaging ) {
fellowtuts_wpbs_pagination($query->max_num_pages);
}
Beautiful Thank you ^_^ 😀 🙂
All
.hidden- classes
have been removed in bs4 beta.https://getbootstrap.com/docs/4.0/migration/#responsive-utilities
@ramiy:disqus yes BS4 beta has removed those classes. I will update the article very soon.
Updated the article.