Changing Ad Unit Div id Dynamically for GAM

Ad Inserter has been very useful plugin for inserting various ads on our website. I am facing issue while inserting an Ad Unit on Post pages using Ad Inserter in Infinite Scroll. I am using Ad inserter to insert Google Ad Manager Ad Units on article pages, everything works fine except the ad unit does not show on next article while the infinite scroll active.

It is the same ad unit as in Normal Article which works absolutely fine but when it is called inside the next article of Infinite Scroll it just don’t show. I think it is due to having same Div id for both units as in First Article and in the next article of infinite scroll.

The solution can be changing the Ad Unit Div id Article dynamically. Can you help me achieve this and provide a solution to change div id dynamically. If you have some better idea which can work to resolve the problem please let me know.

Ad Unit Showing Ad in First Post

Same Ad Unit Not Showing Ad in Next Post in Infinite Scroll

The Ad Unit Code is:

<div id='div-ad-gpt-21619024108-1677680703-0'></div>
	window.googletag = window.googletag || {cmd: []};
	googletag.cmd.push(function() {
		var aff_w = window.innerWidth;

		if (aff_w >= 728) {
			googletag.defineSlot('/21619024108/Nayag_Desktop_AP_MID1_728x90', [[728, 90]], 'div-ad-gpt-21619024108-1677680703-0').addService(googletag.pubads());

			document.getElementById('div-ad-gpt-21619024108-1677680703-0').style.minHeight = 90+"px";
		}   else {
			googletag.defineSlot('/21619024108/Nayag_Mobile_AP_MID1_336x280', [[300, 250], [336, 280]], 'div-ad-gpt-21619024108-1677680703-0').addService(googletag.pubads());

			document.getElementById('div-ad-gpt-21619024108-1677680703-0').style.minHeight = 280+"px";


I am implementing it on test site you can check it here at


  • This topic was modified 8 hours, 11 minutes ago by tricknshop1.
  • This topic was modified 7 hours, 25 minutes ago by James Huff. Reason: link moved to proper field

The page I need help with: [log in to see the link]


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer