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'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);
    if($next_link){
        echo $next_link;
    } else{
        echo 'Next →';
    }
}

Then, I list it as follows:

echo '<ul>';
echo '<li>';
thelink();
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

1 Answer 1

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() {
    ob_start();
    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

 
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.