I am in a bit of dilemma here so bare with me. I want to add something like this code just after the content in a post:

<p>Please let me know what you think by <a href="#comment">leaving a comment</a>!</p>

To do this, I edited single.php and added it after the content. However, the problem is that many of my posts are separated in several pages by using <!--nextpage--> and the above code is displayed on every page. I want that line to be displayed only at the final page if the post is separated. How is such thing possible?


There are some global variables available (or not) to detect the current page number:

if ( empty ( $GLOBALS['multipage'] ) or $GLOBALS['numpages'] === $GLOBALS['page'] )
    echo '<a id="lastPageLink" href="#comment">comment</a>';

The best way to understand what they do is a look at the internals of wp_link_pages().

  • (bool) $GLOBALS['multipage'] is TRUE if there is more than one page.
  • (int) $GLOBALS['numpages'] is the amount of available pages.
  • (int) $GLOBALS['page'] is the number of the current page.

In JavaScript you can add an onclick handler like this now:

document.getElementById("lastPageLink").onclick = function() {
    window.location.hash = 'comment';
    return false;
  • Thank you for updating your answer and detailing each part of the code as I found it unreadable at first. This is great, works as it should! – Christine Cooper Dec 29 '12 at 14:57
  • Somehow, adding this code broke the page echo '<p>What do you think? Let me know by <a href="#comment" onclick="window.location.hash = 'comment'; document.getElementById('comment').focus(); return false;">leaving a comment</a>!</p>'; do you know how I should address this? – Christine Cooper Dec 29 '12 at 15:04
  • 1
    Look at the quotes: You cannot use ' inside of '. – fuxia Dec 29 '12 at 15:06
  • Oh what were I thinking, added backslash before the quotes inside the quotes. Thanks again. – Christine Cooper Dec 29 '12 at 15:07
  • 1
    @ChristineCooper See my update. The reason is the HTML-to-markup ratio and maintainability. You cannot reuse inline scripts. – fuxia Dec 29 '12 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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