How to insert ads every x paragraphs of text [duplicate]

0

I'm using Wordpress (based on php, for those who do not know) and would like to insert a content every x paragraphs. My goal is to put a html or javascript code (an ad from Adsense or direct advertising, for example) every 5 paragraphs (in the middle of the article that the user is reading).

I found this post that presents a solution to this problem in javascript, but for years I stopped programming so I could not make it work on my blog.

Note: The code should add content only to text that is inside the entry-content div. This is the div where the post that the user reads is located.

Edit: The use of ready-made plugins is out of the question.

Edit 2: This is the code I found and partially solved my problem. It does not work for me because with it I can only use text, I can not use an html code, call a page in php or insert a javascript

$('p').each(function(i) {
var pos = i + 1;
if (pos % 3 == 0) {
    $('<div/>', {
        class: 'anuncio',
        text: 'Div inserida!'
    }).insertAfter(this);
   }
});

Edit 3: This is the javascript code (adsense ad) that I need to insert into the page. It is against the Google Adsense rules to even change this code minimally

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-format="fluid"
     data-ad-layout="in-article"
     data-ad-client="ca-pub-6765322619356148"
     data-ad-slot="6356279518"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
    
asked by anonymous 08.08.2017 / 15:55

1 answer

0

After editing the question:

It seems to me that the problem is that you are inserting the script adsbygoogle.js to each ad when in fact you must enter once, just like (adsbygoogle = window.adsbygoogle || []).push({}); must be used only once, which is the part that initializes the ads.

In the example below you see only the <ins></ins> tag being inserted between the paragraphs (open the console to see).

Onasystemsetuptoreceiveads,thisimplementationshouldwork.

$div = $('<ins class="adsbygoogle"   style="display:block; text-align:center;"     data-ad-format="fluid" data-ad-layout="in-article" data-ad-client="ca-pub-6765322619356148" data-ad-slot="6356279518"</ins>');

$div.insertAfter('p:nth-child(3n+0)');

(adsbygoogle = window.adsbygoogle || []).push({});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><p>paragrafo</p><scriptasyncsrc="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    
10.08.2017 / 03:47