SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Rom*ns D'Isonzo, Friuli-Venezia Giulia, Italy, It
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    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:

    Code PHP:
    <?php
      $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>
        <?php 
          include (TEMPLATEPATH . '/inc/meta.php' ); 
          the_content('Read more...'); 
        ?>
      </div>
      <?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; ?>

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Rom*ns D'Isonzo, Friuli-Venezia Giulia, Italy, It
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    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:
    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.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    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:

    PHP Code:
    function append_to_post$content ){
      global 
    $wp_query;
      if( 
    $wp_query->current_post == ) {
        
    $content $content ' Add your banner here';
      }
      return 
    $content;        
    }
    add_filter('the_content''append_to_post'); 

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Rom*ns D'Isonzo, Friuli-Venezia Giulia, Italy, It
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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:

    PHP Code:
    function append_to_post$content ){
      global 
    $wp_query;
      if( 
    $wp_query->current_post == ) {
        
    $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 http://www.myhifi.it, where do I have to look to solve this issue?

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    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.

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Rom*ns D'Isonzo, Friuli-Venezia Giulia, Italy, It
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •