The text for <!--nextpage-->
is hardcoded, so you can't modify that specifically. wp_link_pages
only works with consistent text, so I don't think you can really make that do exactly what you want either, but it does accept a parameter echo
which you can set to false
to return the value. Leveraging that, there are plenty of ways to accomplish what you're looking to do. Three easy options are to use shortcode, another HTML comment, or you could use post meta. Personally, I'd probably opt for another HTML comment, right at the top of each "page". A post might then look like this:
<!--Section:Introduction-->
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<!--nextpage-->
<!--Section:Method-->
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
<!--nextpage-->
<!--Section:Discussion-->
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<!--nextpage-->
<!--Section:Reference-->
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Solution
Add this to your functions.php file and customize it for your usage, then you can call my_wp_link_pages()
instead of wp_link_pages()
to get your custom links:
/**
* Replace page numbers with section titles in HTML comments
*
* @return void
* @author Matthew Boynes
*/
function my_wp_link_pages() {
global $post, $numpages, $multipage;
if ($multipage) {
$links = wp_link_pages(array(
'before' => '<div class="page-link">' . __( 'Pages:', 'toolbox' ),
'after' => '</div>',
'link_before' => '<b>',
'link_after' => '</b>',
'echo' => 0
));
if (preg_match_all('/<!--Section:(.*?)-->/i', $post->post_content, $custom_links)) {
for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
$links = preg_replace("#<b>$i</b>#",$custom_links[1][$i-1],$links);
}
}
echo $links;
}
}
Notes
This uses regular expressions to replace the numbers in the links with the section heading. You're probably wondering about the <b>
tags, right? Those are just to make sure that we identify and replace only the numbers we want to, otherwise there's nothing for the regex to "latch onto" and there would be a significant likelihood for unpredictable behavior (e.g. a slug /juniors-1st-birthday/
might become /juniors-Introductionst-birthday/
).