Forums

Insert (few) ads in between the posts on the homepage (4 posts)

  1. cyanalab
    Member
    Posted 2 years ago #

    So i read the great tutorial who is explaining you how to insert one ad on the homepage : http://codex.wordpress.org/User:Tomhanna/Adding_Ads_to_Wordpress

    so far so good, but i want to insert few ( 3 exactly ) how do i tweak that ?

    HERE IS HOW TO DO IT FOR ONE: 
    
       1. Edit index.php of your current theme.
       2. Look for:
    
          <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    
       3. This is called The Loop. Its purpose is to display posts. Just BEFORE that line, insert this code:
    
          <?php $postnum = 1; $showads = 1; ?>
    
       4. We've just created two variables, $postnum and $showads. The first will be incremented for every new post displayed. The second variable ($showads) is the number of posts you want to display before seeing ads. Next, find this code:
    
          <?php endforeach; else: ?>
    
       5. That line is the end of The Loop. Just BEFORE that line, add this:
    
          <?php if ($postnum == $showads) { ?> <<ADD ADVERTISING HERE>> <?php } $postnum++; ?>
    
       6. Make the appropriate changes such as inserting the advertiser's code where noted. The $showads variable can be changed from '1' to whatever post you want it to display after.
       7. Save the file.

    but if i want to add one ad block after the 3rd post, then 6th and then the 9th posts, how to do it?
    Would love some help on this.

  2. cogmios
    Member
    Posted 2 years ago #

    just add a $counter and then check for the 3rd, 6th, 9th with adding the same code.

  3. cyanalab
    Member
    Posted 2 years ago #

    Cogmios,

    Thank you for your answer, but im not sure i see what you mean?

    Should i do :

    <?php $postnum = 1; $showads = 3,6,9; $counter = 1; ?>

    or

    <?php $postnum = 1; $showads = 3; $counter = 1; ?>
    <?php $postnum = 1; $showads = 6; $counter = 1; ?>
    <?php $postnum = 1; $showads = 9; $counter = 1; ?>

    before the loop, and 3 times the code

    <?php if ($postnum == $showads) { ?> <<ADD ADVERTISING HERE>> <?php } $postnum++; ?>
    <?php if ($postnum == $showads) { ?> <<ADD ADVERTISING HERE>> <?php } $postnum++; ?>

    <?php if ($postnum == $showads) { ?> <<ADD ADVERTISING HERE>> <?php } $postnum++; ?>

    im not super efficient with php as you can see.
    thank you for your help

  4. cogmios
    Member
    Posted 2 years ago #

    I do this out of my head:

    at the top <?php $counter=0 ?>

    in the loop <?php $counter++ ?>

    in the check:
    <?php if ($counter==3 ||$counter==6 ||$counter==9) {
    <<ADD ADVERTISING HERE>>
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic