• Hello,

    This question is about pagination within an individual post (i.e., the single post is too long, so I want to use <!--nextpage--> multiple times).
    I would like my blog index to display the contents of the first page, then display the links to the additional pages. Because I wasn’t happy with the default styling of wp_link_pages(), I followed https://wordpress.stackexchange.com/questions/39622/make-current-active-page-number-a-link-wp-link-pages to create a custom version of wp_link_pages() which allows for CSS styling on the active page number. It’s all working fine on the permalink page. However, on the index, the current page isn’t being detected.

    To see what I mean, compare the bottom of the permalink page (https://www.todaythecity.com/2012/12/23/n-judah-turnaround-beautification-project/) with the bottom of the first post on the index (https://www.todaythecity.com).
    The index shows:
    Page 1 of 3 | 1 | 2 | 3 [1 is linkified]
    The permalink page shows:
    Page 1 of 3 | 1 | 2 | 3 | Next [1 is not linkified]

    How do I get the index to match the permalink page?

    Here is my child theme’s functions.php:

    <?php
    /* Custom link pages from https://wordpress.stackexchange.com/questions/39622/make-current-active-page-number-a-link-wp-link-pages */
    function custom_link_pages($args = '') {
            $defaults = array(
                    'before' => '<p>' . __('Pages:'), 'after' => '</p>',
                    'link_before' => '', 'link_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);
                                    } elseif ( $i == $page ) {
                                        $output .= '<span class="current">';
                                    }
                                    $output .= $link_before . $j . $link_after;
                                    if ( ($i != $page) || ((!$more) && ($page==1)) ) {
                                            $output .= '</a>';
                                    } elseif ( $i == $page ) {
                                        $output .= '</span>';
                                    }
                            }
                            $output .= $after;
                    } else {
                            if ( $more ) {
                                    $output .= $before;
                                    $i = $page - 1;
                                    if ( $i && $more ) {
                                            $output .= _wp_link_page($i);
                                            $output .= $link_before. $previouspagelink . $link_after . '</a>';
                                    }
                                    $i = $page + 1;
                                    if ( $i <= $numpages && $more ) {
                                            $output .= _wp_link_page($i);
                                            $output .= $link_before. $nextpagelink . $link_after . '</a>';
                                    }
                                    $output .= $after;
                            }
                    }
            }
    
            if ( $echo )
                    echo $output;
    
            return $output;
    }
    // Custom Next/Previous Page
    add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');
    /**
     * Add prev and next links to a numbered link list
     */
    function wp_link_pages_args_prevnext_add($args)
    {
        global $page, $numpages, $more, $pagenow;
    
        if (!$args['next_or_number'] == 'next_and_number')
            return $args; # exit early
    
        $args['next_or_number'] = 'number'; # keep numbering for the main part
        if (!$more)
            return $args; # exit early
    
        if($page-1) # there is a previous page
            $args['before'] .= _wp_link_page($page-1)
            . $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'
        ;
    
        if ($page<$numpages) # there is a next page
            $args['after'] = _wp_link_page($page+1)
            . $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
            . $args['after']
        ;
    
        return $args;
    }
    
    ?>

    And here is where I call it:

    <div class="entry-content">
    			<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
    			<?php global $page, $numpages; ?>
    			<?php custom_link_pages( array(
    				'before' => '<div class="page-links"><span>' . __( 'Page '.$page." of ".$numpages."</span> ", 'twentytwelve' ),
    				'after' => '</div>',
    				'link_before'      => '',
    				'link_after'       => '',
    				'next_or_number' => 'next_and_number', # activate parameter overloading
    				'nextpagelink'     => __('Next'),
    				'previouspagelink' => __('Previous'),
    				'pagelink'         => '%',
    				'echo'             => 1)
    					    ); ?>
    		</div>

    Thanks for any input!

  • The topic ‘Pagination for single post: detecting current page is broken on index’ is closed to new replies.