Skip to content

Instantly share code, notes, and snippets.

@fumikito
Last active December 12, 2015 16:34
Show Gist options
  • Save fumikito/f6ad1911e8901ec81d75 to your computer and use it in GitHub Desktop.
Save fumikito/f6ad1911e8901ec81d75 to your computer and use it in GitHub Desktop.
WordPressのWP-Pagenaviが返すHTMLをTwitter Bootstrapに合ったものに変更する
<?php
/**
* Change WP-Pagenavi's output
*
* @package hametuha
* @filter wp_pagenavi
* @param string $html
* @return string
*/
add_filter( 'wp_pagenavi', function($html){
// Remove div.
$html = trim(preg_replace('/<\/?div([^>]*)?>/u', '', $html));
// Wrap links with li.
$html = preg_replace('/(<a[^>]*>[^<]*<\/a>)/u', '<li>$1</li>', $html);
// Wrap links with span considering class name.
$html = preg_replace_callback('/<span([^>]*?)>[^<]*<\/span>/u', function($matches){
if( false !== strpos($matches[1], 'current') ){
// This is current page.
$class_name = 'active';
}elseif( false !== strpos($matches[1], 'pages') ){
// This is page number.
$class_name = 'disabled';
}elseif( false !== strpos($matches[1], 'extend') ){
// This is ellipsis.
$class_name = 'disabled';
}else{
// No class.
$class_name = '';
}
return "<li class=\"{$class_name}\">{$matches[0]}</li>";
}, $html);
// Wrap with ul as you like.
return <<<HTML
<div class="row text-center">
<ul class="pagination pagination-centered">{$html}</ul>
</div>
HTML;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment