How to Display Ads after Entries in WordPress

If you are engaged in your blog, then you might be interested in the question of how to get an ad unit after the first, second or third record on the home page or other, where a list of entries. I want to show the way how to organize such a conclusion after any record. I had a similar article on how to get an advertisement or a block anywhere in the article , who are interested can also read.

Such an example is the withdrawal after a certain post, can be found on the forums, where advertising is placed after a certain response, which in style is framed as a response. Here is the very essence. Maybe you do not want to put a block somewhere at the top or at the bottom of the cycle, and it is beautiful disguise among the records that do not strain your visitors advertising, but at the same time pay attention to it.

To achieve this effect is very easy. First, we will set the counter, and then derive the ad in the right place. I show 2 ways – a conclusion one ad unit after a specific record and display a few blocks after different entries on the account.

Display ad unit after a specific record

You have to decide where it will block the output. Basically this is the main page, so open – the index.php . If you are on another page it is necessary, open the corresponding file. First you need to find the beginning of the cycle. It looks something like this:

<? if (have_posts()) : ?>
<? while (have_posts()) : the_post(); ?>

Do you it can be in a single line, but the point is clear. you need to add them here before the following line:

<?php $counter = 0; ?>

We counter is reset, then you need to find after the withdrawal of the thumbnail of the text, titles, and so the following line:

<?php endwhile; ?>

Before this line insert the code, which will be counted and paste the ad unit.

<?php
$postcount++;
if($postcount==1){?>
<div>// insert here</div>
<?php } ?>

The unit is specified in the third row 1 . This is precisely the number of fasting, after which the ad unit will be displayed. You can change to your desired to achieve.

In the fourth line, insert the correct code of advertising or any element that will be displayed. Nothing complicated.

Conclusion few blocks after the account records at different

The process is very similar to the previous one, so that all in order. Before cycle and paste:

<?php $counter = 0; ?>

Immediately after the loop insert a line like this:

<?php $counter = $counter + 1;?>

You should have something like this:

<?php $counter = 0; ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<?php $counter = $counter + 1;?>

Then, as in the first method before endwhile insert these two blocks:

<?php if(1 == $counter) : { ?>
// insert here
<?php } endif; ?>
<?php if(3 == $counter) : { ?>
// insert here
<?php } endif; ?>

As you can see the first code displays an ad unit after the first record and the second after the third. You can add a third ad unit and its code is:

<?php if(4 == $counter) : { ?>
// insert here
<?php } endif; ?>

It is necessary; just in it in the first line indicate the number of the post, after which the ad unit will be displayed. In fact, these units can do as much as you need.

That’s all you need for effective advertising display in the list of your published posts. Many visitors do not like advertising, but without it, the webmaster cannot monetize their works and get at least a meager profit. To keep your favorite blog and be able to convey useful information to those same visitors.

So, dear webmasters use this method to insert ads on your blog and dear visitors – do not get mad at us, we try not to put a terrible advertising, which covers half of the screen, or yelling at the apartment, it’s time to kill goblins and save Narnia :).

That’s all, thank you for your attention.

One Comment

  1. Jahlin March 4, 2017 Reply

Add a Comment

Your email address will not be published. Required fields are marked *