Header and Footer ads: Only make them visible when 2+ posts on page?

Heey,

I want only to show ads on my multisite blogs in the header and footer, when there is more than 2 or 3 posts on the page. The ad codes is placed in my themes header.php and footer.php. Is it possible with the WP post counter in some way?
Right now i have an ad that does exactly that, but it is in the index.php file, and the theme author told me that it is not possible the same way in the header and footer.

Please help.

  • Patrick
    • Support Monkey

    Hi there @Mathias

    I hope you're well today!

    The theme author is probably correct in saying that the way he has coded his ad function in the index.php template cannot be used in the header.php or footer.php templates. Likely because the post query is present in that template, so the ad function can hook right into it.

    However, that function could, I am sure, be adapted for use in the header or footer templates by hooking it to another function that checks for the template that is actually being used (for example, archive.php), and whether that template is outputting posts.

    Could you perhaps post the contents of that index.php file here so we can take a look? Please enclose the code in code tags by highlighting it and clicking the code button above <...>

  • Patrick
    • Support Monkey

    Hi again @Mathias

    I hope you're well today!

    And thanks very much for your patience on this issue as we deal with an inordinately large number of support tickets these days.

    I just checked those pastes and, indeed, that code cannot be used the same way in the header or footer as it it relies on a counter function right inside the post loop.

    However, after a bit of experimenting on my own test site, it turns out it actually quite simple ro achieve. :slight_smile:

    This function, added to either the header or footer template, will count the number of posts being displayed and, if more than 3, will echo whatever you want:

    <?php
    if (count($posts) > '3'){
    echo ('yay');
    }
    ?>

    Please let us know if this helps, and don't hesitate to post again if you need to.

  • Patrick
    • Support Monkey

    Hi again @Mathias

    That is likely simply due to conflicting quote marks. Please ensure that your code looks like this (with your own publisher Id, etc, of course):

    <?php
    if (count($posts) > '3'){
    echo '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- Test -->
    <ins class="adsbygoogle"
         style="display:inline-block;width:728px;height:90px"
         data-ad-client="ca-pub-1234567890123456"
         data-ad-slot="1234567890"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>';
    }
    ?>
    • Mathias
      • The Bug Hunter

      I actually found out that my code was from AdButler (we use it to control the ads). Sorry about that.

      My code is:

      <!-- PC [async] -->
      <div align="center"><script type="text/javascript">if (!window.AdButler){(function(){var s = document.createElement("script"); s.async = true; s.type = "text/javascript";s.src = 'http://ab165811.adbutler-alion.com/app.js';var n = document.getElementsByTagName("script")[0]; n.parentNode.insertBefore(s, n);}());}</script>
      <script type="text/javascript">
      var AdButler = AdButler || {}; AdButler.ads = AdButler.ads || [];
      var abkw = window.abkw || '';
      var plc173374 = window.plc173374 || 0;
      document.write('<div id="placement_173374_'+plc173374+'"></div>');
      AdButler.ads.push({handler: function(opt){ AdButler.register(165811, 173374, [930,180], 'placement_173374_'+opt.place, opt); }, opt: { place: plc173374++, keywords: abkw, domain: 'ab165811.adbutler-alion.com' }});
      </script>
      <!-- Mobil [async] -->
      <script type="text/javascript">if (!window.AdButler){(function(){var s = document.createElement("script"); s.async = true; s.type = "text/javascript";s.src = 'http://ab165811.adbutler-alion.com/app.js';var n = document.getElementsByTagName("script")[0]; n.parentNode.insertBefore(s, n);}());}</script>
      <script type="text/javascript">
      var AdButler = AdButler || {}; AdButler.ads = AdButler.ads || [];
      var abkw = window.abkw || '';
      var plc173955 = window.plc173955 || 0;
      document.write('<div id="placement_173955_'+plc173955+'"></div>');
      AdButler.ads.push({handler: function(opt){ AdButler.register(165811, 173955, [300,250], 'placement_173955_'+opt.place, opt); }, opt: { place: plc173955++, keywords: abkw, domain: 'ab165811.adbutler-alion.com' }});
      </script></div>

      I use the div's for centering the ads.

    • Mathias
      • The Bug Hunter

      Hi! I actually got this working, but the ads are not showing on the content/posts pages anymore (obviously because there is only one post). I still want the ads to show in the header on the posts pages, because there will always be only one post on that page.

  • Vinod Dalvi
    • WP Unicorn

    Hi Mathia,

    Header and Footer ads: Only make them visible when 2+ posts on page?

    the ads are not showing on the content/posts pages anymore (obviously because there is only one post). I still want the ads to show in the header on the posts pages, because there will always be only one post on that page.

    I want to help you here so i read your whole thread but i found your above two statements confusing posted in this thread.

    Could you please make it clear exactly what you want to achieve?

    Also share the page URLs as examples where you want to display the ads and where you don't want to display it.

    Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.