How to insert ad after nth post on multi post page (functions.php)

I’d like to insert a banner after the first or second post on a multi post page (archives, for example).
I’ve seen the same solution over and over, that is to modify template files or index.php.
I’m using a child theme of a premium theme (Headway), so the best way for me is to add some action or filter inside functions.php. However, I wasn’t able to find a way to do it.
First of all, I am not able to know the number of the current post on the page from inside a filter or action hook function.
Any suggestion?


I was just thinking about how you might implement this. This is what I came up with
Supposing you want to insert a banner after the second post within the loop:

  $counter = 1; 
  if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <div class="entry">
    <h2 class="post_title"><a href="<?php the_permalink(); ?>" title="Read all of &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h2>
      include (TEMPLATEPATH . '/inc/meta.php' ); 
      the_content('Read more...'); 
  <?php if ($counter == 2) : ?>
    // insert your banner here
  <?php endif; ?>
  <?php $counter += 1; ?>  
<?php endwhile; else: ?>
  <p>Unfortunately, no entries were found, which means my site has been hacked and my databases deleted!!</p>
<?php endif; ?>

Thanks, but this must be added to the file where the loop is created. I need some solution that can be added via a filter or an action hook.

Oh right, my bad.
The way I would tackle this then, would be to use a filter on the_content, check each post to see if it is the second post on a multi post page, if it is then simply append your banner to it.
This should do what you want:

function append_to_post( $content ){
  global $wp_query;
  if( $wp_query->current_post == 1 ) {
    $content = $content . ' Add your banner here';
  return $content;		
add_filter('the_content', 'append_to_post');

Thank you, this works. I, indeed, already tried it.
However, it doesn’t work with my theme, a Headway child theme. It works with Twenty Eleven.
The site is at, where do I have to look to solve this issue?

The first thing I would do is to have a look at whatever hooks or filters the Headway parent theme is employing.
You will find these in functions.php.
If you get stuck, let me know.

Thank you.
Sadly Headway generates most of the files at runtime, so the functions.php file is almost empty. I already asked in the support forum, hope I’ll get some answer.