Jump to content

Add Adsense code between paragraphs in an article


Go to solution Solved by Eudemon,

Recommended Posts

Hey there!

I have several advertisements blocks and also blocks widgets that can load these advertisements.

I was wondering... Can I use the {block="myblock"} or {advertisement="myad"} inside a page content? How do you resolve this thing?

Cheers,

Ibai

Link to comment
Share on other sites

10 minutes ago, Eudemon said:

use JS to inject your ad code into post via something like


$("article.cPost:eq(0) [data-role='commentContent'] p:eq(2)").after("your code here")

above shows ad in first post, after 3rd paragraph

Hey thanks @Eudemon!

I understand the code. However, how can I embed adsense code? Just copy and paste their code there escaping the quote mark? Would that work?

Link to comment
Share on other sites

Thanks for the idea @Eudemon

This is my final code for long articles, which I inserted in a advertisement block and then I load wherever I want with {advertisement="myad"}

 

<script>
  const yoyo = '<ins class="adsbygoogle"........ADSENSE INS TAG.......></ins>';
  window.addEventListener('load', function () {
    if($("article:eq(0) p").size()>10){
      $("article:eq(0) p:eq(8)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>25){
      $("article:eq(0) p:eq(20)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>50){
      $("article:eq(0) p:eq(43)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>75){
      $("article:eq(0) p:eq(69)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>100){
      $("article:eq(0) p:eq(98)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>130){
      $("article:eq(0) p:eq(128)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
    if($("article:eq(0) p").size()>170){
      $("article:eq(0) p:eq(167)").after(yoyo);
      (adsbygoogle = window.adsbygoogle || []).push({});
    }
  });
</script>

 

Link to comment
Share on other sites

just few point of improvement base on your code

you can just have one of this, this pushes all adsbygoogle globally

(adsbygoogle = window.adsbygoogle || []).push({});

you can also store `article:eq(0)` to a variable then select whatever within it, so you don't have to repeat the same code, you can also shorten all the statements above with one line tenant condition

Edited by Eudemon
Link to comment
Share on other sites

11 hours ago, Eudemon said:

just few point of improvement base on your code

you can just have one of this, this pushes all adsbygoogle globally


(adsbygoogle = window.adsbygoogle || []).push({});

you can also store `article:eq(0)` to a variable then select whatever within it, so you don't have to repeat the same code, you can also shorten all the statements above with one line tenant condition

Hey Eudemon,

Thanks for your tips. Yeah, definitely the code was optimizable, that was the first approach. I applied your second suggestion.

However, are you sure that one line of push is enough? I've been testing and when I code just one line of push, it shows only the last ad. I put back that line for each ad and now is showing all of them.

 

Thanks for your support!

Link to comment
Share on other sites

  • 7 months later...
On 3/17/2021 at 4:50 AM, Ibai said:

Hey Eudemon,

Thanks for your tips. Yeah, definitely the code was optimizable, that was the first approach. I applied your second suggestion.

However, are you sure that one line of push is enough? I've been testing and when I code just one line of push, it shows only the last ad. I put back that line for each ad and now is showing all of them.

 

Thanks for your support!

It's not working more 😞

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...