Inserting ads after (or before) posts on the home page

Discussion in 'CSS and PHP' started by SteveS, Sep 8, 2011.

  1. SteveS Steve

    After searching Google, there are a lot of different variations of doing this. I was unable to add more than 4 ads or text lines total. The first section I used Google Adsense, the other section I used something else.

    Here's what I do. I edit NomNom: Main Index Template (index.php) and right after this line
    Code:
    <?php while ( have_posts() ) : the_post(); ?>
    I insert this code.
    Code:
    <?php /* INSERT AFTER POST 1 AND POST 2 */ ?>
    <?php if ($var == 1 || $var == 2) : ?>
    <div align="center">
    My ad or text goes HERE!
    </div>
    <?php endif; $var++; ?>
    
    <?php /* INSERT AFTER POST 3 AND POST 4 */ ?>
    <?php if ($count == 3 || $count == 4 ) : ?>
    <div align="center">
    My ad or text goes HERE!
    </div>
    <?php endif; $count++; ?>
    If you want the ads to display before the post, then insert the ad before the have_posts php code.
  2. Zeaks Author

    Thanks for this Steve, for some reason i was trying to add the code to content.php, no wonder I couldn't get it to work right.

    Here's what I'm using. In Index.php under the
    PHP:
    <?php while ( have_posts() ) : the_post(); ?>
    Add
    PHP:
        <?php /* increment the counter */ ?>
        <?php if($count == || $count== 2) : ?>
        <?php endif; ?>
    Then under
    PHP:
    <?php get_template_part'content'get_post_format() ); ?>
    Add
    PHP:
    <?php get_template_part'content'get_post_format() ); ?>
    <?php 
    /* Calls ads-1.php if exists */ ?>
        <?php if ($count == 0) : ?>
    <?php get_template_part
    'ads-1'$count ); ?>
        <?php endif; $count ++; ?>
    <?php 
    /* Calls advert-2.php */ ?>
        <?php if ($count == 2) : ?>
    <?php get_template_part
    'ads-2'$count ); ?>
    I created 2 files, ads-1.php and ads-2.php and put my adsense code in them and they display under the first and second post. Pretty much the same thing, just calling template files instead.

Share This Page