enter image description here

I am using WordPress and I’m trying to create a custom wp_link_pages() function to customize how it looks in my theme.

The function is used to display a list of the article’s pagination whenever I use the page break.

The output looks like this

As you can see, part 1 and part 2 are on the same line and they are not being wrapped in li while part 3 is inside li

The HTML output of the function looks like this

<ul id="post-pagination"> 
  <a href="http://localhost/awan/blog/we-graduate-to-dine-together-2-days-later/">Part 1</a> 
  <a href="http://localhost/awan/blog/we-graduate-to-dine-together-2-days-later/2/">Part 2</a> 
  <li class="active">Part 3</li>
</ul>

I would like the first 2 anchor tags to also be wrapped inside a list-item tag but without the active active.

This is my function

function custom_wp_link_pages( $args = '' ) {
    $defaults = array(
        'before' => '<ul id="post-pagination">' . __( '' ),
        'after' => '</ul>',
        'text_before' => 'Part ',
        'text_after' => '',
        'next_or_number' => 'number',
        'nextpagelink' => __( 'Next page' ),
        'previouspagelink' => __( 'Previous page' ),
        'pagelink' => '%',
        'echo' => 1
    );

    $r = wp_parse_args( $args, $defaults );
    $r = apply_filters( 'wp_link_pages_args', $r );
    extract( $r, EXTR_SKIP );

    global $page, $numpages, $multipage, $more, $pagenow;

    $output = '';
    if ( $multipage ) {
        if ( 'number' == $next_or_number ) {
            $output .= $before;
            for ( $i = 1; $i < ( $numpages + 1 ); $i = $i + 1 ) {
                $j = str_replace( '%', $i, $pagelink );
                $output .= ' ';
                if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) )
                    $output .= _wp_link_page( $i );
                else
                    $output .= '<li class="active">';

                $output .= $text_before . $j . $text_after;
                if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) )
                    $output .= '</a>';
                else
                    $output .= '</li>';
            }
            $output .= $after;
        } else {
            if ( $more ) {
                $output .= $before;
                $i = $page - 1;
                if ( $i && $more ) {
                    $output .= _wp_link_page( $i );
                    $output .= $text_before . $previouspagelink . $text_after . '</a>';
                }
                $i = $page + 1;
                if ( $i <= $numpages && $more ) {
                    $output .= _wp_link_page( $i );
                    $output .= $text_before . $nextpagelink . $text_after . '</a>';
                }
                $output .= $after;
            }
        }
    }

    if ( $echo )
        echo $output;

    return $output;
}

Read more here: Wrap anchor tags in list-item tags in a PHP function in WordPress


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: