Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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';
    document.getElementById('comment').focus();
    return false;
}
share|improve this answer
    
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 '. –  toscho 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. –  toscho Dec 29 '12 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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