Add a Class to Current Page WP_LINK_PAGES

I’ve noticed WordPress has some illogical differences between wp_link_pages and paginate_links. Specifically, WP_LINK_PAGES does not automatically add a class to the current page like paginate_links does.

Anybody have a quick functions fix?

Related posts

Leave a Reply

1 comment

  1. This is what I use as a replacement for wp_link_pages(). Instead of a separate class it uses another element for the current page, because a page should never link to the current URL.

    /**
     * Modification of wp_link_pages() with an extra element to highlight the current page.
     *
     * @param  array $args
     * @return void
     */
    function t5_numerical_link_pages( $args = array () )
    {
        $defaults = array(
            'before'      => '<p>' . __( 'Pages:', 't5_theme' )
        ,   'after'       => '</p>'
        ,   'link_before' => ''
        ,   'link_after'  => ''
        ,   'pagelink'    => '%'
        ,   'echo'        => 1
        // element for the current page
        ,   'highlight'   => 'b'
        );
    
        $r = wp_parse_args( $args, $defaults );
        $r = apply_filters( 'wp_link_pages_args', $r );
        extract( $r, EXTR_SKIP );
    
        global $page, $numpages, $multipage, $more, $pagenow;
    
        if ( ! $multipage )
        {
            return;
        }
    
        $output = $before;
    
        for ( $i = 1; $i < ( $numpages + 1 ); $i++ )
        {
            $j       = str_replace( '%', $i, $pagelink );
            $output .= ' ';
    
            if ( $i != $page || ( ! $more && 1 == $page ) )
            {
                $output .= _wp_link_page( $i ) . "{$link_before}{$j}{$link_after}</a>";
            }
            else
            {   // highlight the current page
                // not sure if we need $link_before and $link_after
                $output .= "<$highlight>{$link_before}{$j}{$link_after}</$highlight>";
            }
        }
    
        $echo and print $output . $after;
        return $output . $after;
    }