Skip to content

Instantly share code, notes, and snippets.

@novrian
Last active September 19, 2018 08:09
Show Gist options
  • Save novrian/6001818 to your computer and use it in GitHub Desktop.
Save novrian/6001818 to your computer and use it in GitHub Desktop.
Wordpress Pagenavi Helper to display pagenavi like Twitter Bootstrap Pagination
<?php
/**
* Twitter Bootstrap Pagenavi
*
* This is tweaks function to display WP-Pagenavi markup that displayed like
* Twitter Bootstrap Pagination.
*
* Do not fille the pagenavi's options below:
* - Text For Number Of Pages
* - Text For `First` / `Last Page`
* - Text For `Previous ...` / `Next ...`
*
* Usage:
* Just put this code on your theme's function.php
*
* @author Novrian Nono <me@novrian.info>
* @param string $size Bootstrap Pagination Size: `normal | small | large | mini`
* @param string $position Bootstrap Pagination Position: `left | centered | right`
* @param boolean $post set it to true on multipart post
* @param object WP_Query Object if you use custom query
*/
function nofx_wp_pagenavi($size = null, $position = null, $post = false, $queryArgs = null) {
if (!function_exists('wp_pagenavi')) {
return null;
}
$class[] = "pagination"; // Set Main Class
// Pagination Size Class
if (!$size) {
$size = '';
}
$class[] = (!$size) ? '' : "pagination-" . $size;
// Pagination Position, default LEFT
if (!$position) {
$position = 'left';
}
$class[] = "pagination-" . $position;
// Set Before & After Output
$before = "<div class=\"" . implode(" ", $class) . "\"><ul>";
$after = "</ul></div>";
// Build Args
$args = array(
'before' => $before,
'after' => $after
);
// Cloning untuk wp_link_pages()
if ($post) {
$args['type'] = 'multipart';
}
if ($queryArgs) {
$args['query'] = $queryArgs;
}
return wp_pagenavi($args);
}
/**
* This is filter that help above template tags
*/
function nofx_pagenavi_filter($html) {
$out = str_replace('<div class=\'wp-pagenavi\'>', '', $html);
$out = str_replace('</div></ul></div>', '</ul></div>', $out);
$out = str_replace('<a ', '<li><a ', $out);
$out = str_replace('</a>', '</a></li>', $out);
$out = str_replace('<span', '<li><a href="#"><span', $out);
$out = str_replace('</span>', '</span></a></li>', $out);
$out = preg_replace('/<li><a href="#"><span class=[\'|"]pages[\'|"]>([0-9]+) of ([0-9]+)<\/span><\/a><\/li>/', '', $out);
$out = preg_replace('/<li><a href="#"><span class=[\'|"]extend[\'|"]>([^\n\r<]+)<\/span><\/a><\/li>/', '<li class="disabled"><a href="#">&hellip;</a></li>', $out);
$out = str_replace('<li><a href="#"><span class=\'current\'', '<li class="active disabled"><a href="#"><span class="current"', $out);
return $out;
}
add_filter('wp_pagenavi', 'nofx_pagenavi_filter', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment