0

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.

4
  • Why do you have if ($echo) towards the end of the script? Commented Mar 9, 2014 at 0:05
  • Print out $ndx I bet it's 0 every time. Instead just keep track of $pageNumber and increment it each iteration Commented Mar 9, 2014 at 0:06
  • @Floris This is a function I pulled from somewhere else. I believe the $echo is tied to the WordPress wp_link_pages() function this is meant to augment. A boolean switch to either print the links or return a variable. Commented Mar 9, 2014 at 8:26
  • @ElefantPhace it's definitely not 0 every time. The generated href attributes are correct, and those rely on the same variable. Also, as noted in the commented code, I printed the variable as a test before and after the line in question and confirmed that the value was correct. The problem was tied to the isset() call. --See Mark Dunphy's awnser. Commented Mar 9, 2014 at 8:32

1 Answer 1

3

Your logic is off. Think carefully about the line that sets $title.

$title = isset( $title ) && ( strlen( $title ) > 0 ) ? $title : 'Page '.$pageNumber;

When $title isn't set (the first iteration, where $pageNumber is 1), you set $title to "Page 1". From that point on, $title is set and will not change. What you're basically saying is "If I have already set $title to a value, set it equal to itself".

Sign up to request clarification or add additional context in comments.

1 Comment

That's it exactly, thank you. It seems embarrassingly obvious now that you've pointed it out (which is what I expected would happen)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.