1

Does anyone know if it's possible to change wp_link_pages, so that the title of each nextpage quicktag is used rather than numbers? I can't seem to find anything on this.

This is an example of how I'm using the quicktag:

<!--nextpage--><!--pagetitle:Overview-->

Using wp_link_pages(); it displays/outputs; Pages: 1 2

I would prefer it to display/output; Pages: Overview | 2nd Page Title | 3rd Page Title

Where it takes the <!--pagetitle:Overview--> and uses this as the link to the next page.

Any ideas/help greatly appreciated, S.

2
  • There isn't a next page, just a next part of the same page, the title in each case is going to be the same isn't it? Essentially it's one page in many parts, so the title won't differ, will it?
    – t31os
    Commented Mar 9, 2011 at 14:06
  • Yes, I understand what you're saying. I want to use the pagetitle that I define next to each <!--nextpage-->.
    – sta777
    Commented Mar 9, 2011 at 14:25

3 Answers 3

3

This is going to be kind of hackish, but it is possible using an alternate function.

Add this to your theme's functions.php file.

function wp_link_pages_titled($args = '') {
    $defaults = array(
        'before' => '<p>' . __('Pages:'), 
        'after' => '</p>',
        'link_before' => '', 
        'link_after' => '',
        'echo' => 1
    );

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

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

    $output = '';
    if ( $multipage ) {
        $output .= $before;
        for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
            $part_content = $pages[$i-1];
            $has_part_title = strpos( $part_content, '<!--pagetitle:' );
            if( 0 === $has_part_title ) {
                $end = strpos( $part_content, '-->' );
                $title = trim( str_replace( '<!--pagetitle:', '', substr( $part_content, 0, $end ) ) );
            }
            $output .= ' ';
            if ( ($i != $page) || ((!$more) && ($page==1)) ) {
                $output .= _wp_link_page($i);
            }
            $title = isset( $title ) && ( strlen( $title ) > 0 ) ? $title : 'First';
            $output .= $link_before . $title . $link_after;
            if ( ($i != $page) || ((!$more) && ($page==1)) )
                $output .= '</a>';
        }
        $output .= $after;
    }
    if ( $echo )
        echo $output;
    return $output;
}

Now update each call you have to wp_link_pages to instead call on the new function wp_link_pages_titled.

It's not perfect, and it is kind of hacky, but it will do what you asked, just be sure you use this format when writing them into your posts..

Content intro

<!--nextpage--><!--pagetitle: Title 1 -->

Next page content 1

<!--nextpage--><!--pagetitle: Title 2 -->

Next page content 2

<!--nextpage--><!--pagetitle: Title 3 is longer -->

Next page content 3

<!--nextpage--><!--pagetitle: Another Title -->

Next page content 4

You can also title the link to the first page, but it must appear before anything else, ie. right on the first line, eg..

<!--pagetitle: Title for very first link -->
Content intro

Code was written with a mind to just getting it work, there's room for improvement, i mainly wanted to illustrate it would be possible, it's up to you(or anyone else) to expand upon it.

Hope that helps get things rolling for you.. ;)

3
  • That is awesome! Thank you so much! I couldn't find anything on this anywhere - hopefully others will find this useful as well.
    – sta777
    Commented Mar 9, 2011 at 16:14
  • You're welcome... :) I intend to improve on it when i get time(or bored)... Until then, i hope it's a good starting point for you(and anyone else)... :)
    – t31os
    Commented Mar 9, 2011 at 18:30
  • We should combine our scripts and build a function wp_link_pages_reloaded(). :)
    – fuxia
    Commented Apr 14, 2011 at 15:07
0

Great solution - this really helps make the PageNavi plugin work how I would hope.

One tweak I did to the code was change "First" to "Introduction," (you could replace intro with anything else depending on your usage.) Adding the comma you will also need to do at the end of the page titles.

$title = isset( $title ) && ( strlen( $title ) > 0 ) ? $title : 'First';

So this:

Introduction First Look Conclusions

Becomes this: (which is a better formatting)

Introduction, First Look, Conclusions

So, little more than a formatting tweak, but helps the readability. I didn't get the included code for the first page to work, so hard coded it into the function. Not pretty, but effective since I always call the first page "Introduction."

0

Great solution! But I'm having trouble using this function. If my page look like this

Content intro
<!--nextpage--><!--pagetitle: Title 1 -->
Next page content 1
<!--nextpage--><!--pagetitle: Title 2 -->
Next page content 2
<!--nextpage--><!--pagetitle: Title 3 is longer -->
Next page content 3
<!--nextpage--><!--pagetitle: Another Title -->
Next page content 4

It look s great, but if

Content intro
<!--nextpage-->
Next page content 1
<!--nextpage--><!--pagetitle: Title 2 -->
Next page content 2
<!--nextpage-->
Next page content 3
<!--nextpage--><!--pagetitle: Another Title -->
Next page content 4

the result is terrible! I made ​​a few changes to fix it (changes only)

        if( 0 === $has_part_title ) {
            $end = strpos( $part_content, '-->' );
            $title = trim( str_replace( '<!--pagetitle:', '', substr( $part_content, 0, $end ) ) ).', ';
        } else { $title = $i.', ';}

It seems now workig better! ;-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.