用于在文章内容中手动插入 AdSense 广告的代码
ARTRU
虽然AdSense支持在文章内容中插入广告,但效果不是很好。由于随机插入广告,网站布局经常被破坏。
目前,我在 Oxygen Builder 中使用此 PHP 代码每 7 行加载 AdSense 广告,特别是 7 个标签。 <p>
.
第 1 步:关闭自动广告。
第 2 步:创建文章内广告单元广告。
步骤 3:将 javascript 代码添加到页脚部分。
在第四行: var paragraphs = document.querySelectorAll('.wp-content p');
请更换 .wp-content
等于文章内容元素的类。每个主题都不同,因此您需要使用 Devtool (F12) 检查。
记得改”xxxxxx
” 根据您刚刚在步骤 2 中创建的脚本添加到您自己的代码中。
<script>
document.addEventListener("DOMContentLoaded", () => {
var count = 0;
var paragraphs = document.querySelectorAll('.wp-content p');
for (var i = 0; i < paragraphs.length; i++) {
count++;
if (count % 7 === 0) {
var ad_in_content = document.createElement('ins');
ad_in_content.setAttribute('class', 'adsbygoogle');
ad_in_content.setAttribute('style', 'display:block; text-align:center;');
ad_in_content.setAttribute('data-ad-layout', 'in-article');
ad_in_content.setAttribute('data-ad-format', 'fluid');
ad_in_content.setAttribute('data-ad-client', 'ca-pub-xxxxxx');
ad_in_content.setAttribute('data-ad-slot', 'xxxxxx');
paragraphs[i].insertAdjacentElement('afterend', ad_in_content);
}
}
const ad_script = document.createElement("script");
ad_script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxx";
ad_script.setAttribute("crossOrigin", "anonymous");
document.body.appendChild(ad_script);
ad_script.addEventListener("load", function () {
document.querySelectorAll(".adsbygoogle").forEach(function (ad_script) {
(window.adsbygoogle = window.adsbygoogle || []).push({});
});
});
});
</script>
相关文章