How to Insert AdSense Ads in WordPress Loop (Code + No-Code Methods)
Want to show AdSense ads between posts on your WordPress homepage, archive, or category pages? There are two ways: write a few lines of PHP, or use a plugin. Both work. I’ll show you each method so you can pick the one that fits your comfort level.
I’ve been running AdSense on WordPress sites since 2010. The loop insertion method is one of the highest-earning placements because the ad sits between content the reader is actively scrolling through. It blends in naturally and gets better CTR than sidebar or footer ads.
What Is the WordPress Loop?
The WordPress Loop is the PHP code that pulls posts from the database and displays them on archive pages (homepage, category, tag, search results, author pages). It runs through each post one by one. When you “insert an ad in the loop,” you’re telling WordPress to display an ad unit after the 2nd, 3rd, or Nth post in that list.
This is different from inserting ads inside a single post’s content. Loop ads appear between posts on listing pages. Single post ads appear within the article body. Both are valid AdSense placements, but this guide focuses on loop ads specifically.
Method 1: Insert AdSense in WordPress Loop with PHP (Code Method)
This is the cleanest approach. No plugin overhead, no compatibility issues, and you control exactly where the ad appears. You’ll need access to your theme files, either through a child theme or a code snippets plugin.
Step 1: Get Your AdSense Ad Unit Code
Log into your Google AdSense account. Go to Ads > By ad unit > Display ads. Create a new ad unit (responsive recommended). Copy the ad code. It looks like this:
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="1234567890"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Step 2: Add the Ad Function to Your Theme
Add this code to your child theme’s functions.php file. Replace the placeholder values with your actual AdSense publisher ID and ad slot number.
/**
* Display AdSense ad unit.
* Replace ca-pub and data-ad-slot with your values.
*/
function gt_display_loop_ad() {
?>
<div class="loop-ad" style="margin: 2rem 0; text-align: center;">
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="1234567890"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</div>
<?php
}
Step 3: Insert the Ad Inside the Loop
Open your theme’s template file that handles the loop. This is usually index.php, archive.php, home.php, or category.php. Find the WordPress Loop (the while ( have_posts() ) block) and add a counter.
Here’s the complete pattern to show an ad after the 3rd post:
<?php
$post_count = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_count++;
// Display your post content
get_template_part( 'template-parts/content', get_post_format() );
// Show ad after the 3rd post
if ( $post_count === 3 ) {
gt_display_loop_ad();
}
endwhile;
endif;
?>
Show Ads at Multiple Positions
Want to show ads after the 3rd and 6th posts? Use an array of positions:
<?php
$post_count = 0;
$ad_positions = array( 3, 6 ); // Show ad after 3rd and 6th posts
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_count++;
get_template_part( 'template-parts/content', get_post_format() );
if ( in_array( $post_count, $ad_positions, true ) ) {
gt_display_loop_ad();
}
endwhile;
endif;
?>
Show an Ad Every Nth Post
To repeat an ad every 4th post throughout the loop, use the modulus operator:
<?php
$post_count = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_count++;
get_template_part( 'template-parts/content', get_post_format() );
// Show ad every 4th post
if ( $post_count % 4 === 0 ) {
gt_display_loop_ad();
}
endwhile;
endif;
?>
AdSense policy reminder: Google allows up to 3 AdSense display ad units per page. If your loop shows 10+ posts, don’t place an ad after every 2nd post. Stick to 2-3 in-loop ads maximum to stay within policy and avoid a bad user experience.
Advanced: Using WordPress Hooks Instead of Editing Templates
If your theme supports the the_post action hook (most modern themes do), you can inject ads without editing template files at all. Add this to functions.php:
/**
* Insert AdSense ad after specific post positions in the loop.
* Works with any theme that uses the standard WordPress Loop.
*/
function gt_adsense_in_loop( $post ) {
global $wp_query;
// Only run on archive/home pages, not single posts
if ( is_singular() ) {
return;
}
$position = $wp_query->current_post;
// Show ad after the 3rd post (position 2, zero-indexed)
if ( $position === 2 ) {
gt_display_loop_ad();
}
}
add_action( 'the_post', 'gt_adsense_in_loop' );
This method is cleaner because you don’t touch any template file. It works across all archive pages automatically.
Method 2: Insert AdSense in WordPress Loop with Plugins (No-Code Method)
Don’t want to write PHP? These plugins let you insert AdSense ads between posts without touching a single line of code.
Ad Inserter (Free)
Ad Inserter is the most powerful free ad management plugin for WordPress. It supports 16 code blocks, each with granular placement controls.
To insert an AdSense ad between posts in the loop:
- Install and activate Ad Inserter from the WordPress plugin repository.
- Go to Settings > Ad Inserter.
- Paste your AdSense code in one of the 16 available blocks.
- In the Insertion dropdown, select “Before Post” or “After Post.”
- Under Lists, check “Homepage”, “Category pages”, “Archive pages” (wherever you want loop ads).
- Set the “Filter” to control which post number triggers the ad (e.g., after 3rd post).
- Save and clear your cache.
Ad Inserter also handles A/B testing, ad rotation, and lazy loading out of the box. It’s free for up to 16 blocks, which is more than enough for most sites.
WPCode (Free)
WPCode (formerly Insert Headers and Footers) is the simplest way to add code snippets to WordPress without editing theme files. It’s not a dedicated ad plugin, but it handles loop ad insertion well.
- Install and activate WPCode.
- Go to Code Snippets > Add Snippet.
- Choose “Add Your Custom Code (New Snippet)” and select PHP as the code type.
- Paste the PHP hook method from Method 1 above (the
gt_adsense_in_loopfunction and thegt_display_loop_adfunction). - Toggle it active, save, and you’re done.
WPCode is the best option if you want the reliability of the PHP method but don’t want to create a child theme. Your snippets survive theme switches and updates.
Google Auto Ads (Fully Automatic)
This is the zero-effort option. Google’s Auto Ads uses machine learning to figure out where to place ads on your site, including between posts in the loop. You don’t choose positions. Google does.
- In your AdSense dashboard, go to Ads > By site.
- Click the pencil icon next to your site.
- Toggle Auto ads ON.
- Choose which ad formats to allow (in-page, anchor, vignette, etc.).
- Set Ad load to control density (lower = fewer ads, better UX).
- Click Apply to site.
The AdSense script (already on your site if you have any manual ads) handles everything. Auto Ads will test different placements and optimize over time.
My recommendation: Use Auto Ads alongside 1-2 manual loop placements. Manual ads in proven positions (after 3rd post, after 6th post) give you a baseline. Auto Ads fills in additional spots where Google’s algorithm thinks they’ll earn.
Which Method Should You Use?
| Method | Best For | Pros | Cons |
|---|---|---|---|
| PHP code (functions.php) | Developers, theme customizers | Full control, no plugin overhead, fastest | Requires child theme or code snippets plugin |
| WordPress hooks (the_post) | Developers who want clean code | No template editing, works across all archives | Still requires PHP knowledge |
| Ad Inserter plugin | Non-coders who want control | Visual interface, A/B testing, ad rotation | Another plugin to maintain |
| WPCode plugin | Anyone adding code snippets | Survives theme switches, simple UI | Still uses PHP under the hood |
| Google Auto Ads | Hands-off publishers | Zero setup, Google optimizes placement | Less control over exact positions |
AdSense Loop Ad Placement Best Practices
I’ve tested in-loop ad placements across 30+ sites over the years. Here’s what I’ve learned:
- After the 3rd post is the sweet spot. The reader is engaged but not overwhelmed. First and second positions feel too aggressive.
- Responsive display ads outperform fixed sizes. Let Google pick the optimal size for each device. Don’t hardcode 728×90 or 300×250 for loop ads.
- Limit to 2-3 ads per page. More ads means lower CPM because ad inventory exceeds demand. It also slows page load and hurts your Core Web Vitals.
- Lazy load ads below the fold. The first in-loop ad (position 3) loads normally. Ads at position 6+ should lazy load. Both Ad Inserter and manual code support this.
- Don’t show loop ads on mobile if your archive layout is compact. If each post excerpt is only 2-3 lines, an ad after every 3rd post makes the page feel like an ad farm.
- Test with and without. Use a caching plugin that supports separate mobile/desktop caches so you can A/B test placements without affecting page speed.
What About Block Themes and Full Site Editing?
If you’re using a block theme with Full Site Editing (WordPress 6.0+), your archive templates are built with the Query Loop block in the Site Editor. There’s no index.php or traditional PHP loop to edit.
For block themes, your options are:
- Ad Inserter plugin still works. It hooks into the rendered output regardless of whether the loop is PHP or block-based.
- Google Auto Ads works everywhere since it’s client-side JavaScript.
- Custom HTML block inside the Query Loop template in the Site Editor. You can manually add an AdSense code block between Post Template iterations, but this is limited and awkward.
- The PHP hook method (
the_postaction) works even with block themes because block themes still use WP_Query under the hood.
For block theme users, I’d recommend the the_post hook method via WPCode, or Ad Inserter. Both work without touching template files.
Performance Impact of In-Loop Ads
Each AdSense ad unit adds roughly 50-80KB to your page weight and triggers 3-5 additional network requests. On a page with 3 loop ads, that’s 150-240KB extra and 9-15 requests. Not trivial.
To minimize the hit:
- Load the main
adsbygoogle.jsscript withasync(it already ships this way). - Lazy load ads below the fold using the Intersection Observer API or a plugin like Perfmatters or FlyingPress that can delay scripts until user interaction.
- Use a caching plugin so the page HTML is served from cache, reducing the server-side impact to near zero.
- Don’t load AdSense on pages with no ads. If you only show loop ads on the homepage, conditionally enqueue the script.
Troubleshooting Common Issues
Ads not showing: New AdSense accounts can take 24-48 hours to serve ads. Also check that your site’s ads.txt file is correctly configured (AdSense dashboard > Sites > your site).
Ads showing on single posts instead of archives: Make sure your code includes the is_singular() check to prevent loop ads from firing on single post pages.
Layout shift (CLS issues): AdSense responsive ads don’t have a fixed height, which causes layout shifts. Add a min-height to your ad container: min-height: 250px for display ads. This reserves space before the ad loads.
Ads not appearing after theme update: If you edited template files directly (not using a child theme), theme updates overwrite your changes. Switch to the hook method or WPCode to make your ad code update-proof.
FAQs
How many AdSense ads can I show in the WordPress loop?
Google removed the 3-ad-per-page limit in 2026, but that doesn’t mean you should fill every gap. Practically, 2-3 in-loop ads is the sweet spot. More than that tanks your page speed, lowers CPM (oversupply), and creates a bad user experience that increases bounce rate.
Do I need a child theme to add AdSense code to the loop?
Not if you use the WordPress hook method (the_post action) via WPCode or a similar snippets plugin. The hook method doesn’t require editing template files, so it survives theme updates and switches. If you do edit template files directly, yes, use a child theme.
Will in-loop AdSense ads slow down my WordPress site?
Each ad unit adds 50-80KB and 3-5 network requests. With 2-3 ads per page, expect roughly 200KB additional page weight. Use lazy loading for ads below the fold and a caching plugin to serve static HTML. The performance impact is manageable with proper optimization.
Can I insert AdSense ads in the loop with a block theme?
Yes. Block themes still use WP_Query under the hood, so the the_post hook method works. Alternatively, use Ad Inserter plugin or Google Auto Ads, both of which work regardless of your theme type. You can also add a Custom HTML block inside the Query Loop template in the Site Editor.
Should I use Auto Ads or manual loop placement?
Use both. Set up 1-2 manual loop ads in proven positions (after the 3rd and 6th post) as your baseline. Enable Auto Ads on top of that to let Google fill additional placements. Manual ads give you control; Auto Ads adds incremental revenue from positions you wouldn’t think to test.
Wrapping Up
The PHP hook method is my preference for in-loop AdSense ads. It’s 15 lines of code, works with every theme (classic and block), doesn’t require a plugin, and gives you exact control over ad positions. If code isn’t your thing, Ad Inserter or WPCode get you the same result through a UI.
Whatever method you pick, start with one ad after the 3rd post. Monitor your AdSense RPM for two weeks, then experiment with a second placement. Small, measured changes beat loading up the page with ads and hoping for the best.
If you’re looking to go beyond AdSense, check out my guide on the best AdSense alternatives and diversifying your blog income beyond display ads.