Print

How to Add Google AdSense Between Posts in WordPress

Step 1: Edit template file

You can display Google AdSense ads (image ads or text ads) between your posts on your post listing page like categories or archive pages or your main index page which is called the homepage or frontpage.

To do so log into your WordPress dashboard and go into your theme editor screen available from Appearance > Editor
or much better use a FTP editor like filezilla to do the modifications.  This is the recommended way because if you make any syntax error in worst case your website will be unavailable at all. Doing the modification via FTP allows you to revert it any time.

If you like to do it like a pro, create a staging site and do all those modifications first on a test site. If everything is working well, copy the changes back to the live site.
You can use the free plugin WP Staging to do so.

Open the template file that is responsible for output. If you want to change the front page, edit the file home.php, frontpage.php or index.php
The file name depends on the theme you are using.

Find the line that says:

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

Below that line add the code:

// WP QUADS
global $wp_query;
if( shortcode_exists('quads') && $wp_query->current_post === 1 ){
echo do_shortcode( '' );
}

Other relevant template file names are archive.php or category.php. If you are unsure what file name the correct one ask your theme developer

Warning: If you have any code error in the  code it’s highly likely that the change will break your website. So do it on a staging site first

To prevent breaking your website:

  • Do the changes on a testing site first. You can create a clone of your site very easily with WP STAGING.
  • Copy the original code first and paste it into Notepad or another text editor program. If something goes wrong you can put the original code back into the original file by opening it with a FTP program.

Step 2:  Enter Your WP QUADS Ad Code

Now you can add as many ads as you want by entering the following code below the have_posts() code anywhere in the content.

Example – Add ad2 after post 1:

<?php global $wp_query;
if( shortcode_exists('quads') && $wp_query->current_post === 0 ){
echo do_shortcode( '' );
} ?>

Example – Add ad2 after post 6:

<?php global $wp_query;
if( shortcode_exists('quads') && $wp_query->current_post === 6 ){
echo do_shortcode( '' );
} ?>

Note, that current_post is starting counting at 0!

Step 3: View Your Blog Frontpage

Now check out your blog frontpage to ensure that the changes you made display exactely as you want them to. Note that a live ad might not appear immediately, but the position placeholder should be there right away. It can take Google a day or more to start displaying contextually relevant ads in a new ad unit.