There is a site on wordpress , in the block of output of posts of which there is a query_posts query and a standard while (have_posts()) : the_post(); .

  <?php query_posts('showposts=6&cat=98,100,97,99'); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> ... <?php endwhile; ?> <div class="home-post-block"> <?php echo adrotate_ad(1);?> </div> <div class="home-post-block"> <?php echo adrotate_ad(2);?> </div> <div class="home-post-block"> <?php echo adrotate_ad(3);?> </div> <?php endif; ?> 

I have the adrotate banner ad adrotate , whose ads I want to display in the same block if they are active

  <div class="home-post-block"> <?php echo adrotate_ad(2);?> </div> 

Question: how to do this technically? I think that somehow, but something is not right here ...

 <?php $count=0; if(adrotate_ad(1) && adrotate_ad(2) && adrotate_ad(3) { $count=3; } else if (adrotate_ad(1) && adrotate_ad(2)) { $count=4; } else if (adrotate_ad(2) && adrotate_ad(3)) { $count=4; } else if (adrotate_ad(1) && adrotate_ad(3)) { $count=4; } else if (adrotate_ad(1) || adrotate_ad(2) || adrotate_ad(3)) { $count=5; } else { $count=6; } query_posts('showposts=$count&cat=98,100,97,99'); ?> 

    1 answer 1

    Why not count in a cycle how many ad units are actively

     <?php $adcount = 0; for ($ix = 1; $ix < 4; $ix += 1) { if (adrotate_ad($ix)) $adcount += 1; } $count = 6 - $adcount; query_posts('showposts=' . $count . '&cat=98,100,97,99'); ?> 

    Supplemented . Due to the new circumstances (the adrotate_ad() argument is the id block) I propose the following:

     <?php $adcount = 0; $adIds = array(7, 8, 19, 20); foreach ($adIds as $id) { if (adrotate_ad($id)) $adcount += 1; } $count = 6 - $adcount; query_posts('showposts=' . $count . '&cat=98,100,97,99'); ?> 
    • it’s just that in general there are more ad units in general than I want to display specifically here and they are disjointed: I need to bring the blocks under id 7,8,19,20 and in the future I would not like dependencies if the blocks change or replenish with other id - Vasya
    • @ Vasya This information should have been given in the text of the question. Updated the answer - tutankhamun
    • and yet somewhere an error does not deduce what is needed. First of all, the variable showposts=$count&cat=98,100,97,99 does not work exactly in the request. I'm trying to get out through the conditional series, but I also have an error somewhere: 2 ads must display 4 posts + 2 ads ($ count correctly shows 4, $ adcount - 2), but for some reason, 2 posts are displayed. - Vasya
    • if ($ count == 6) {query_posts ('showposts = 6 & cat = 98,100,97,99'); } elseif ($ count == 5) {query_posts ('showposts = 5 & cat = 98,100,97,99'); } elseif ($ count == 4) {query_posts ('showposts = 4 & cat = 98,100,97,99'); } elseif ($ count == 3) {query_posts ('showposts = 3 & cat = 98,100,97,99'); } elseif ($ count == 2) {query_posts ('showposts = 2 & cat = 98,100,97,99'); } if you directly substitute 4, then 4 is output, as it should be - Vasya
    • one
      @ Vasya A variable is not substituted because you used single quotes, and I stupidly copied the request. I sprinkle my head with ashes. Fixed the answer - tutankhamun