Jump to content

Community

Add Adsense code between paragraphs in an article


Ibai
 Share

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

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

image.png.32cd17aeb4d39706c36ea32e622dff0b.png

Edited by Eudemon
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

  • Solution
Posted (edited)
7 minutes ago, Ibai said:

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

yes

const yoyo = '<b>tofu is life</b>'
$("article.cPost:eq(0) [data-role='commentContent'] p:eq(2)").after(yoyo)

image.png.82ada16bed0b416b0dc865a94c89fa22.png

Edited by Eudemon
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

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We use technologies, such as cookies, to customise content and advertising, to provide social media features and to analyse traffic to the site. We also share information about your use of our site with our trusted social media, advertising and analytics partners. See more about cookies and our Privacy Policy