I am trying to replace and add some value in the HTML class… I am success in solving this issue to some extent but now I need help. Here is my function code
JavaScript
x
function wpse247219_custom_pagination() {
global $the_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('page') ),
'total' => $the_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => true,
'prev_text' => __( 'Previous', 'text-domain' ),
'next_text' => __( 'Next page', 'text-domain'),
) );
$output = '';
if ( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );
$output .= '<ul class="pagination-list">';
foreach ( $pages as $page ) {
$output .= "<li>$page</li>";
}
$output .= '</ul>';
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );
$xpath = new DOMXpath( $dom );
$page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );
foreach ( $page_numbers as $page_numbers_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 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'page-numbers',
'pagination-link',
$page_numbers_item->attributes->item(0)->value );
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'prev pagination-link',
'pagination-previous',
$page_numbers_item->attributes->item(0)->value );
}
// Save the updated HTML and output it.
$output = $dom->saveHTML();
}
return $output;
}
this code currently output this modified Html Code
JavaScript
<nav class="pagination is-rounded" role="navigation" aria-label="pagination">
<ul class="pagination-list">
<li>
<a class="pagination-previous" href="https://blog.igsavers.com/page/3/">Previous</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/1/">1</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/2/">2</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/3/">3</a></li>
<li><span aria-current="page" class="page-numbers current">4</span></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/5/">5</a></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/6/">6</a></li>
<li><span class="pagination-link dots">…</span></li>
<li><a class="pagination-link" href="https://blog.igsavers.com/page/11/">11</a></li>
<li><a class="next pagination-link" href="https://blog.igsavers.com/page/5/">Next page</a></li></ul>
</nav>
And Now I Also want to replace the class of <span class="page-numbers current">
to <span class="pagination-link is-current ">
so that it looks like this
I am also trying to remove <li></li>
tag from the next and previous page button. any type of help will greatly be appreciated
Advertisement
Answer
After a some experiment with my code I solved my question. Here is my code…
JavaScript
function wpse247219_custom_pagination() {
global $the_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('page') ),
'total' => $the_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => true,
'prev_text' => __( 'Previous', 'text-domain' ),
'next_text' => __( 'Next page', 'text-domain'),
) );
$output = '';
if ( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );
$size = count($pages)-1;
$output .= '<ul class="pagination-list">';
$i = 0;
foreach ( $pages as $page ) {
if($i < 1 || $i == $size){
$output .= "$page";
}else{
$output .= "<li>$page</li>";
}
$i++;
}
$i = 0;
$output .= '</ul>';
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );
$xpath = new DOMXpath( $dom );
$page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );
foreach ( $page_numbers as $page_numbers_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 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'page-numbers',
'pagination-link',
$page_numbers_item->attributes->item(0)->value );
// Replace the class 'page-numbers' with 'page-link'
$page_numbers_item->attributes->item(0)->value = str_replace(
'prev pagination-link',
'pagination-previous',
$page_numbers_item->attributes->item(0)->value );
}
$yupo = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers current')]" );
foreach($yupo as $yup){
$yup->attributes->item(1)->value = str_replace(
'page-numbers current',
'pagination-link is-current',
$yup->attributes->item(1)->value );
}
// Save the updated HTML and output it.
$output = $dom->saveHTML();
}
return $output;
}