这个paginate_links()
功能,位于wp-includes/general-template.php
, 不允许HTML的某些部分(例如page-numbers
类)进行定制。
由于类的生成方式,简单的字符串替换将不起作用,这在paginate_links()
:
$page_links[] = \'<a class="prev page-numbers" href="\' . esc_url( apply_filters( \'paginate_links\', $link ) ) . \'">\' . $args[\'prev_text\'] . \'</a>\';
endif;
for ( $n = 1; $n <= $total; $n++ ) :
if ( $n == $current ) :
$page_links[] = "<span class=\'page-numbers current\'>" . $args[\'before_page_number\'] . number_format_i18n( $n ) . $args[\'after_page_number\'] . "</span>";
这是您的
custom_pagination()
函数,它使用DOMXpath查找具有
page-numbers
类,然后执行字符串替换以将类更改为
page-link
. 为了更好地与引导程序兼容
current
类也替换为
active
. 最后
class="mynewclass"
已添加到
<li>
其中包含当前项。
function wpse247219_custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages,
\'prev_next\' => false,
\'type\' => \'array\',
\'prev_next\' => true,
\'prev_text\' => __( \'«\', \'text-domain\' ),
\'next_text\' => __( \'»\', \'text-domain\'),
) );
$output = \'\';
if ( is_array( $pages ) ) {
$paged = ( get_query_var(\'paged\') == 0 ) ? 1 : get_query_var( \'paged\' );
$output .= \'<ul class="pagination">\';
foreach ( $pages as $page ) {
$output .= "<li>$page</li>";
}
$output .= \'</ul>\';
// Create an instance of DOMDocument
$dom = new \\DOMDocument();
// Populate $dom with $output, making sure to handle UTF-8, otherwise
// problems will occur with UTF-8 characters.
$dom->loadHTML( mb_convert_encoding( $output, \'HTML-ENTITIES\', \'UTF-8\' ) );
// Create an instance of DOMXpath and all elements with the class \'page-numbers\'
$xpath = new \\DOMXpath( $dom );
// http://stackoverflow.com/a/26126336/3059883
$page_numbers = $xpath->query( "//*[contains(concat(\' \', normalize-space(@class), \' \'), \' page-numbers \')]" );
// Iterate over the $page_numbers node...
foreach ( $page_numbers as $page_numbers_item ) {
// Add class="mynewclass" to the <li> when its child contains the current item.
$page_numbers_item_classes = explode( \' \', $page_numbers_item->attributes->item(0)->value );
if ( in_array( \'current\', $page_numbers_item_classes ) ) {
$list_item_attr_class = $dom->createAttribute( \'class\' );
$list_item_attr_class->value = \'mynewclass\';
$page_numbers_item->parentNode->appendChild( $list_item_attr_class );
}
// Replace the class \'current\' with \'active\'
$page_numbers_item->attributes->item(0)->value = str_replace(
\'current\',
\'active\',
$page_numbers_item->attributes->item(0)->value );
// Replace the class \'page-numbers\' with \'page-link\'
$page_numbers_item->attributes->item(0)->value = str_replace(
\'page-numbers\',
\'page-link\',
$page_numbers_item->attributes->item(0)->value );
}
// Save the updated HTML and output it.
$output = $dom->saveHTML();
}
return $output;
}
Usage:
echo wpse247219_custom_pagination();
Generated HTML:
<ul class="pagination">
<li class="mynewclass"><span class="page-link active">1</span></li>
<li><a class="page-link" href="http://localhost/page/2/">2</a></li>
<li><a class="page-link" href="http://localhost/page/3/">3</a></li>
<li><span class="page-link dots">…</span></li>
<li><a class="page-link" href="http://localhost/page/5/">5</a></li>
<li><a class="next page-link" href="http://localhost/page/2/">»</a></li>
</ul>