Inject fragment after x number of posts in loop


Hi guys,

I am needing to inject a fragment of code after the 3rd post in the loop. I thought there might be a filter I can use, but I can see anything in the code output.

Any ideas?

Thanks, Chris


Hello @chris,

Is this something like that that you're searching for ?

// Add something after third post in the main loop.
add_action( 'beans_post_after_markup', 'mythemeprefix_add_something' );
function mythemeprefix_add_something() {
 global $wp_query;
 $counter = $wp_query->current_post;
 if ( 2 == $counter ) {
    ?>
        <!--SOMETHING-->

    <?php

 }
}

mathieu


Hey Mathiew,

I actually just got it working using a custom loop (which is what I was trying to avoid), but your solution is a lot more elegant πŸ™‚

Thanks! Chris


To be honest, it is very much inspired by a @thierry's snippet ! πŸ™‚


Ah, hadn't seen that one. Thanks again for sharing it! πŸ™‚


Mathieu Rouault@ Thanks I was also searching for a solution like this πŸ™‚

Write a reply

Login or register to write a reply, it's free!