Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / WordPress

How to Place Ad Scripts in Between Post Listings on Index.php in WordPress

5.00/5 (1 vote)
16 Sep 2014CPOL3 min read 10.1K  
How to place ad scripts in between post listings on Index.php in WordPress

Now, you just recently migrated from WordPress.com to WordPress.org and on your old WordPress you have AdWords installed, now you want to have that similar ad placement on your new WordPress install by placing the ads on the header, on the widget side bar and after the first post summary. The widget is quite easy so we will not discuss that and for the header we discussed this yesterday so refer to this post if needed . What we are going to concentrate on today is placing that third ad in between the first post excerpt and the second post excerpt.

Now let me tell you this now, it will vary depending on your theme and we will discuss them all.

First, if your theme shows post lists on one column, then repeats on the rows, then it's quite simple. You just need to identify where the post loop is being executed on your index.php and it should look something similar to this:

PHP
<!--?<?php while ( have_posts() ) : the_post(); ?>

Above that, you need to declare a trigger so copy the first line of code below and paste it above the while loop.

PHP
<!--?<?php $j = 1; ?>     
<!--?<?php while ( have_posts() ) : the_post(); ?>

Now search for this code:

PHP
<!--?<?php get_template_part( 'content', get_post_format() ); ?>

inside the while loop, then just beneath it, paste the following:

PHP
<!--?<?php 
if ($j == 1)
{
    echo 'place your ad script here';
    $j = 2;
}
;?>

Now just replace that text that contains ‘place your ad script here’ with your chosen ad script. What this piece of code does is it just writes the script one time as the variable $j increments and will not equal to one on succeeding loops, but on the next page, it will reset showing your ad again after the first excerpt.

If you successfully coded it, it might look like this:

PHP
<!--?<?php $j = 1; ?>     
<!--?<?php while ( have_posts() ) : the_post(); ?>
            
<!--?<?php get_template_part( 'content', get_post_format() ); ?>
    <?php 
    if ($j == 1)
    {
        echo 'place your ad script here';
        $j = 2;
    }
    ;?>
                    
<!--?<?php endwhile; ?>

and your output will be like this:

01 After first post

Now what if your theme uses featured posts, this will definitely behave differently as the loop only starts on the second item, meaning your ad will show only after the second excerpt having a high possibility it’s not above the fold.

So how do you deal with it?

Before we start, you need to know that featured post only shows on the first page so you need to consider them. Now let's find the features post entry on index.php, it should look like this:

PHP
<!--?<?php get_template_part('inc/featured'); ?>

Now, you can place your ads below it but only if it’s the front page, you need to put that condition because if not, it will show directly below the header and before the first excerpt on page 2, 3, 4, ….

Here is how you do it.

PHP
<!--?<?php if ($_SERVER["REQUEST_URI"] == '/' )
{
    echo 'place your ad script here';
}
?>

and this is how the output looks like:

02 After Featured

Now how about the succeeding pages? All you need to do is something similar on our first approach above but with a condition before that telling that it should not be the front page.

PHP
if ($j == 1 && !($_SERVER["REQUEST_URI"] == '/' )) 
{ 
    echo 'place your ad script here';
    $j = 2;
}

Now let’s go to the harder one. What if your excerpts show on two columns like a grid?

Well, there is a bit of calculation that you will see on the codes and you need to make use of that. Let's take this for an example:

PHP
<div class="post-list group">
    <?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?>
    <?php get_template_part('content'); ?>
    <?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?>
</div><!--/.post-list-->

As you can see, the post breaks after 2 items so in this case you need to insert your code somewhere in between and it will look like this:

PHP
<div class="post-list group">
    <?php $i = 1; $j = 1; echo '<div class="post-row">';  while ( have_posts() ): the_post();  ?>
    <?php get_template_part('content');  ?>
    <?php if($i % 2 == 0) 
    { 
        if ($j == 1 && !($_SERVER["REQUEST_URI"] == '/' )) 
        { 
            echo 'place your ad script here';
            $j = 2;
        } 
        else 
        { 
            echo '</div><div class="post-row">';
        } 
    } 
    $i++;  
    endwhile; 
    echo '</div>'; ?>
                
</div><!--/.post-list-->

Once coded correctly, it would look something like this:

03 After featured next pages


All you need to do with the code above is change add your script.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)