2

I have a plugin where I want to output ads after X number of posts on the home page. The home page is step 1, but things like archives should be possible too once I get the code for the home page.

How do I hook in to post loops and say something like "after every loop, increment a counter, and then if the counter = my number, output an ad". I can write all the logic for this code myself, but where to hook/implement my code is confusing.

  • You want to add it through the the_content filter? Can you post what you tried so far? Are you going to publish this plugin or is it just for your own homepage? – birgire Apr 14 '14 at 17:44
  • This is part of a very large, complicated code plugin. It's for serving ads, and it's already in place and live on many customers sites. the_content sounds fine. I just want to output a shortcode after specific post counts. For example, on the home page, put ads after posts 1, 3, and 5. All I need to know is how to hook into that location, then I can use the_content to add something below the post. – Branndon Apr 14 '14 at 18:33
3

You can try the following:

Method 1:

We can inject the ads via the the_post action, in the main loop:

add_action( 'loop_start', 'wpse_141253_loop_start' );

function wpse_141253_loop_start( $query )
{
    if( $query->is_main_query() )
    {
        add_action( 'the_post', 'wpse_141253_the_post' );
        add_action( 'loop_end', 'wpse_141253_loop_end' );
    }
}

function wpse_141253_the_post()
{
    static $nr = 0;

    if( 0 === ++$nr % 4 )
        echo '<div> -------- MY AD HERE ------- </div>';
}

function wpse_141253_loop_end()
{
    remove_action( 'the_post', 'wpse_141253_the_post' );   
}    

Method 2:

We can also inject the ads via the the_content filter, in the main loop:

add_action( 'loop_start', 'wpse_141253_loop_start' );

function wpse_141253_loop_start( $query )
{
    if( $query->is_main_query() )
    {
        add_filter( 'the_content', 'wpse_141253_the_content' );
        add_action( 'loop_end', 'wpse_141253_loop_end' );
    }
}

function wpse_141253_the_content( $content )
{
    static $nr = 0;

    if( 0 === ++$nr % 4 )
        $content .= '<div>------- MY AD HERE -------</div>';

    return $content;
}

function wpse_141253_loop_end()
{
    remove_action( 'the_post', 'wpse_141253_the_content' );   
}    

Hopefully you can modify this to your needs.

|improve this answer|||||
  • I'll test this and let you know. Thanks for the detailed answer! I'll be sure to vote up and mark as solved if this works for me. – Branndon Apr 14 '14 at 21:07
  • I see that you have left a little code on method 2 though. I don't think I need to use the loop_end functions, do I? – Branndon Apr 14 '14 at 21:09
  • Too bad I can't vote up your answer, not enough points yet. It worked for me using your second method. – Branndon Apr 14 '14 at 21:14
  • Ok, great. I think it's a good house-keeping rule, to clean up everything in the end. So that's why I added the loop_end part ;-) – birgire Apr 14 '14 at 21:18
  • Right, but in method two, there is no function wpse_141253_the_post, maybe you meant wpse_141253_the_content ? – Branndon Apr 14 '14 at 21: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.