I'm trying to generate customized link text for paginated WordPress posts.
Using <!--nextpage--><!--pagetitle: My Title-->, I identify a page break and set a title for the next page.
The title is used as the text in the generated link (rather than the WordPress default --plain numbers).
Calling wp_link_pages_titled() will generate the custom links and print them on the page.
The function works as expected, and generates the correct links, but I'm having trouble with the fallback used when <!--pagetitle: My Title--> is not present.
When no title is specified, I want to revert back to using 'Page [number]' as the link text. Unfortunately, the fall back is printing the incorrect number.
To test the function, I created a post with 3 paginated sections, and none of these sections have a title.
The links should be printed as
- Page 1
- Page 2
- Page 3
But are instead printing as
- Page 1
- Page 1
- Page 1
Note: The href attribute of the anchor is correct, only the displayed text is wrong.
I've included the foreach loop below. I tried to comment the PHP as much as possible to describe what's happening.
// For each paginated section
foreach ( $pages as $ndx => $part_content) {
$pageNumber = $ndx + 1;
// Check to see if the pagetitle quick tag is present
$has_part_title = strpos( $part_content, '<!--pagetitle:' );
if( $has_part_title ) {
$tagEnd = strpos( $part_content, '-->' );
$title = trim( str_replace( '<!--pagetitle:', '', substr( $part_content, 0, $tagEnd ) ) );
}
$output .= ' ';
// Generate a link to the $pageNumber -- Will not generate link for the current page
if ( ($pageNumber != $page) || ((!$more) && ($page==1)) ) {
$output .= _wp_link_page($pageNumber); // Page number is correct here - the link generated is accurate
}
echo 'Page Number: '.$pageNumber.' -- Link Generated <br /> '; // This is a test to check the value of $pageNumber. Value prints as expected
// If the pagetitle is present, use it. If not, pring 'Page [$pageNumber]'
$title = isset( $title ) && ( strlen( $title ) > 0 ) ? $title : 'Page '.$pageNumber;
$output .= $link_before . $title . $link_after;
echo 'Page Number Again: '.$pageNumber.' -- Title Generated <br> '; // This is a test to check the value of $pageNumber. Value prints as expected
// Close the generated link -- will not execute when $pageNumber == the current page
if ( ($pageNumber != $page) || ((!$more) && ($page==1)) ) {
$output .= '</a>';
}
}
// Close the "list"
$output .= $after;
}
/* Worth noting: Links are added to a single output string and printed in one go after the foreach executes.
Seems like it would be better to print each link individually */
if ( $echo )
echo $output; // When $output prints, the link text for every page is 'Page 1' for some reason, the page number here is not displayed correctly
return $output;
Why does the echo $pageNumber; print the correct value, but the 'Page '.$pageNumber print only 'Page 1' for every link?
I'm sure I've missed something obvious, but I can't place what it is. Any help is greatly appreciated.
if ($echo)towards the end of the script?$ndxI bet it's 0 every time. Instead just keep track of$pageNumberand increment it each iteration