Method of displaying Adsense below on each article on WordPress

I arranged the advertisement without using the plug-in according to the desire though it was general to use the plug-in to arrange the article below in each article on the word press.

The Google AdSense for contents is up to 3 pieces per page.

When the advertisement of Google Adsense is displayed in each article on WordPress, it becomes the following trouble. When the number of displayed articles becomes four or more, the code of Google Adsense becomes four or more. This violates the program policy that AdSense for contents are permitted up to three.

Moreover, this is not displayed when is behind the code of the advertisement of the side. It is not possible to display it anyway well.

Method of displaying advertisement only in the first article and the third article

The method of displaying Google Adsense only in the first article on the blog of WordPress and the third article was tried. Because it was possible to display it well, it introduces it. As for the plug-in, it doesn't want to use as much as possible, and this is easier though it is likely to be able to do even if the plug-in is used.

Of course, the advertisement can be displayed only in the first and 2nd article by the setting. Moreover, the advertisement can be displayed only in the first article. The display of the advertisement is freely revokable like this.

Index.php of the theme of WordPress is edited. There are the following parts in index.php.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Here are the existing code.
<?php comments_template(); // Get wp-comments.php template ?>

The place where it wants to display the AdSense advertisement adds the following code next to the first line of the record on the former Japanese oak of the article before the last above-mentioned line if it is the back of the article.

<?php $ad_count++; ?>
<?php if ($ad_count==1|$ad_count==3) {
print <<<EOD
Past here the Adsense code.
EOD;
} ?>

It is the above, the AdSense advertisement is on the first article and the 3rd article or it is displayed only in the under. How many it displays it must change the figure of "$ad_count==1" suitably.

It specifies it as follows when displaying it only on the first and 2nd article or under.

<?php if ($ad_count<=2) {

An advertising code is made a foreign file, and it reads by include.

Of course, you do not care even if the AdSense code etc. are made a foreign file and it reads to index.php with include though you may write the AdSense code directly in index.php. This might be smarter. In this case, it writes in the place where index.php inserts the following codes.

<?php include ('adsense.php'); ?>

Adsense.php is put on the same directory as the theme. Any extension of this file is not cared about. To display the advertisement under the article (back) when concretely explaining, as follows is done.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Here are the existing code.
<?php include ('adsense.php'); ?>
<?php comments_template(); // Get wp-comments.php template ?>

The content of adsense.php does as follows.

<?php $ad_count++; ?>
<?php if ($ad_count==1|$ad_count==3) {
print <<<EOD
Past here the Adsense code.
EOD;
} ?>

The advertisement of the other companies can be similarly displayed.

It is possible not to limit to the Google AdSense, and to display it in the place of hope automatically even in optimism and the advertisement of Amazon. Because an advertising code becomes a foreign file as for this method, management is easy. Moreover, because the advertisement is recorded in in the data base but the theme, the backup is easy. However, please take care at the upgrade of the theme.