How to insert ads every x text paragraphs

Asked

Viewed 720 times

0

I am 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 an html or javascript code (an Adsense ad or direct advertising, for example) every 5 paragraphs (in the middle of the article the user is reading).

I found that post which presents a solution to this problem in javascript, but it’s been years since I stopped programming so I couldn’t make it work on my blog.

Note: Code should only add content in text that is within the div entry-content. This is the div where the post 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 that partially solves my problem. It doesn’t work for me because with it I can only use text, I can’t use 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) I need to enter on the page. It is against the rules of Google Adsense to change even q minimally this code

<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>
  • Hello Pablo. Try looking for a plugin that does the insertion of advertisements, it is an easier way to deal with this problem.

  • 1

    post here your code so we can see what might be wrong

  • Matthew, I don’t like using plugins because it makes the site heavy and my hosting is not the best.

  • Tmc, I never came up with a code. There is this one that I saw in another topic (link in the initial post), but it is in javascript and accepts only text, in my case I need something that accepts html and javascript: &#xA;$('p').each(function(i) {&#xA; var pos = i + 1;&#xA; if (pos % 3 == 0) {&#xA; $('<div/>', {&#xA; class: 'anuncio',&#xA; text: 'Div inserida!'&#xA; }).insertAfter(this);&#xA; }&#xA;});&#xA;

  • 1

    @Pablo the other answer to the question you lynched brings the solution to your problem. Instead of creating a div passing an object as second parameter you can directly pass the full html of the div, which can contain any content.

  • @Ricardomoraleida I know that the basis of the problem was solved in the other topic but I really could not make it work (even in the other topic the person had problem msm, only could insert text). Could you send me the full code? I tried to change the content there from "text" to "html" and put the javascript code of the Adsense ad, but it didn’t work. I was able to only use pure html as bold, font color, etc., but not javascript or php code. I even downloaded some javascript and php booklets to re-program but this takes time and I have urgency.

  • 1

    This link was in the first reply comment and has a functional example of how to pull scripts using the same code as your question: https://jsfiddle.net/sn0zz83v/

  • @I saw this code too, I fought with him but I couldn’t make it work. I edited the main post with the script code I need to insert, if you can take a look I appreciate it. Sorry and ignorance, I know q doesn’t make much sense to be in a programmers forum without knowing how to program minimally but it is q to really without alternatives.

  • 1

    @Pablo doesn’t have to be embarrassed, everyone starts from somewhere. The problem with this post is that the question you asked was already answered. This is the main exercise to be done. If the answer to the question you asked does not answer you, it is a sign that the question to be asked must be another. After editing the problem became clearer and it is possible to help you.

Show 4 more comments

1 answer

0

After editing the question:

It seems to me that the problem is that you are inserting the script adsbygoogle.js when in fact you should insert only once, as well as the (adsbygoogle = window.adsbygoogle || []).push({}); should be used only once, which is the part that initializes the ads.

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

inserir a descrição da imagem aqui

On a system set up to receive announcements this implementation should work.

$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>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.