WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm checking if the next post is available in the same category. If it is, output the link. If it's not, output a static text. I have created a function for this:

function thelink() {
    $next_link = next_post_link('%link', 'Next →', TRUE);
        echo $next_link;
    } else{
        echo 'Next →';

Then, I list it as follows:

echo '<ul>';
echo '<li>';
echo '</li>';
echo '</ul>';

The problem is, the output includes both the link AND the static text. It does check for the posts though. Here's what happens:

If this is NOT the last post, the output is as following, rendering the Next twice. Once with the link, followed by static text:

<li><a href="urlofthenextpage" rel="next">Next →</a>Next →</li>

If this is the last post, the output is just static text:

<li>Next →</li>

I realize that the problem seems to be in the loop. But I'm out of ideas on how to get this working. Help is VERY much appreciated.

share|improve this question
up vote 1 down vote accepted

The problem is that next_post_link actually prints the link.

If you want to do this as described above, you could use output buffering:

function thelink() {
    next_post_link('%link', 'Next &rarr;', TRUE);
    $next_link = ob_get_clean();
    if ($next_link) echo $next_link;
    else echo 'Next &rarr;';
share|improve this answer
Thank you very much t f! That worked! – Aftab Mar 28 '13 at 10:43

Your Answer


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.