Last active
May 19, 2020 08:30
-
-
Save joelstransky/86f7b77f89fc24851f64e7b7bd6df368 to your computer and use it in GitHub Desktop.
Display pagination in WordPress! using Twitter Bootstrap 3's pagination component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! function_exists('paginate_links_as_bootstrap') ) : | |
/** | |
* paginate_links_as_bootstrap() | |
* https://gist.github.com/joelstransky/86f7b77f89fc24851f64e7b7bd6df368 | |
* Wraps paginate_links data in Twitter bootstrap pagination component | |
* @param array $args { | |
* Optional. {@see 'paginate_links'} for native argument list. | |
* | |
* @type string $nav_class classes for <nav> element. Default empty. | |
* @type string $ul_class additional classes for <ul.pagination> element. Default empty. | |
* @type string $li_class additional classes for <li> elements. | |
* } | |
* @return string|void String of page links. | |
*/ | |
function paginate_links_as_bootstrap ( $args = '' ) { | |
$args['type'] = 'array'; | |
$defaults = array( | |
'nav_class' => '', | |
'ul_class' => '', | |
'li_class' => '' | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$page_links = paginate_links( $args ); | |
if ( $page_links ) { | |
$r = ''; | |
$nav_class = empty($args['nav_class']) ? '' : 'class="' . $args['nav_class'] . '"'; | |
$ul_class = empty($args['ul_class']) ? '' : ' ' . $args['ul_class']; | |
$r .= '<nav '. $nav_class .' aria-label="navigation">' . "\n\t"; | |
$r .= '<ul class="pagination'. $ul_class .'">' . "\n"; | |
foreach ($page_links as $link) { | |
$li_classes = explode(" ", $args['li_class']); | |
strpos($link, 'current') !== false ? array_push($li_classes, 'active') : ( strpos($link, 'dots') !== false ? array_push($li_classes, 'disabled') : '' ); | |
$class = empty($li_classes) ? '' : 'class="' . join(" ", $li_classes) . '"'; | |
$r .= "\t\t" . '<li ' . $class . '>' . $link . '</li>' . "\n"; | |
} | |
$r .= "\t</ul>"; | |
$r .= "\n</nav>"; | |
return $r; | |
} | |
} | |
endif; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array( | |
"nav_class" => "some-nav-class some-other-nav-class", | |
"ul_class" => "some-ul-class some-other-ul-class", | |
"li_class" => "some-li-class some-other-li-class" | |
); | |
echo paginate_links_as_bootstrap( $args ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment