wp_link_page – wrap current page element

I’m trying to make something more meaningful out of the wp_link_pages() result:

$paged_page_nav = wp_link_pages( array( 'echo' => false ) );
// Now let's wrap the nav inside <li>-elements
$paged_page_nav = str_replace( '<a', '<li class="'.$classes.'"><a', $paged_page_nav );
$paged_page_nav = str_replace( '/a>', '/a></li>', $paged_page_nav );

// here I'd need to wrap the currently displayed page element

echo '<ul>'.$pages_page_nav.'</ul>';

Currently my link pages (numbered nav/pagination) displays like this:

<!-- assuming we display page(d) 2/5 -->
<ul>
    <li><a href="https://wordpress.stackexchange.com/questions/17421/...">1</a></li>
    2 <!-- This is the currently displayed paged, wrapped in nothing -->
    <li><a href="https://wordpress.stackexchange.com/questions/17421/...">3</a></li>
    <li><a href="https://wordpress.stackexchange.com/questions/17421/...">4</a></li>
    <li><a href="https://wordpress.stackexchange.com/questions/17421/...">5</a></li>
</ul>

2 Answers
2

I wouldn’t use WordPress’ internal function.
We had a very similar question recently: How to style current page number (wp_link_pages)? I wrote a small but flexible function to replace wp_link_pages(). It is probably easier to extend this than hacking around the return value of the native function.

Leave a Comment