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 want to add a link to the first post of every WP blog that i have. For example this simple function adds the link to every post.

    function add_link($link){

    $link .= '<a href="http://site.com">Link</a>';
return $link;
    }
    add_filter('the_content','add_link');

so basically instead of the link showing below each post, I want it to show only at the bottom of the first post only.

thanks.

share|improve this question
    
Still not solved? –  toscho Oct 6 '12 at 22:09
    
not yet... thanks. –  Ronny Oct 7 '12 at 19:30

2 Answers 2

up vote 2 down vote accepted

Just remove the filter when it is called the first time:

function add_link($link)
{
    // this will guarantee the filter will not be called again.
    remove_filter( current_filter(), __FUNCTION__ );
    return $link . '<a href="http://site.com">Link</a>';
}
share|improve this answer

Trivial with some basic logic

Add before your function:

$added = false;

Then check e.g.:

    if(!$added ){
        $added = true;
        $link .= '<a href="http://site.com">Link</a>';
    }
share|improve this answer
    
I think you mean if (!$added). Also it's not really clear where this $added variable is scoped. @toscho's answer is much cleaner. –  David John Smith Jan 20 '13 at 3:59

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.