How to place ad scripts in between post listings on index.php in WordPress

By | June 19, 2014

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 going to concentrate 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 depend 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 its 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 while ( have_posts() ) : the_post(); ?>

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

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

Now search for this code

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

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

<?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 next page it will reset showing your ad again after the first excerpt.


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

<?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 lets find the features post entry on index.php it should look like this

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

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

Now let’s go to the harder one.  What if you 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.  Lets take this for an example

<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++; endwhileecho '</div>'?>
</div><!--/.post-list-->

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

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


Leave a Reply