Zmień klasę „numery stron” w paginacji — WordPress Development Stack Exchange
The paginate_links()
funkcja, zlokalizowana w wp-includes/general-template.php
nie pozwala na pewne części kodu HTML (takie jak page-numbers
klasa) do dostosowania.
Prosta zamiana ciągu nie będzie działać ze względu na sposób generowania klas, jak podkreślono w tym fragmencie kodu 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>";
Oto zaktualizowana wersja pliku custom_pagination()
funkcja, która używa ścieżki DOMXpath do do wyszukiwania elementów instancji za pomocą page-numbers
class, następnie następuje zamiana ciągu znaków w celu zmiany klasy na page-link
. Aby uzyskać lepszą kompatybilność z Bootstrap, plik current
klasa jest również zastępowana przez active
. Wreszcie, class="mynewclass"
jest dodawany do <li>
który zawiera bieżący element.
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 );
//
$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;
}
Stosowanie:
echo wpse247219_custom_pagination();
Wygenerowany kod HTML:
<ul class="pagination">
<li class="mynewclass"><span class="page-link active">1</span></li>
<li><a class="page-link" href="
<li><a class="page-link" href="
<li><span class="page-link dots">…</span></li>
<li><a class="page-link" href="
<li><a class="next page-link" href="
</ul>