3

There are many scripts/hacks out there to insert ads into the middle of the post (after 'x' number of paragraphs), however I haven't found a good enough script that can do the above.

This is the script that comes close,

<?php $post_counter=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php require('post.php'); ?>
<?php
$post_counter++;
if ($post_counter == 2 || $post_counter == 3) { ?>
INSERT ADSENSE CODE HERE
<?php } ?>
<?php endwhile; ?>

Taken from here. But my index.php uses this instead:

<?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('cat=-3&showposts=6&paged='.$page_num); ?>
            <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php get_template_part('includes/index-loop'); ?>
            <?php endwhile; wp_reset_postdata(); wp_reset_query(); ?>
            <div class="clear"></div>

        <?php else : ?>
        <?php endif; ?>

I tried putting in the

<?php
$post_counter++;
if ($post_counter == 2 || $post_counter == 3) { ?>
INSERT ADSENSE CODE HERE
<?php } ?>

before endwhile, but I see a repetition of 'INSERT ADSENSE CODE HERE' twice. Any suggestions?

  • "I see a repetition of 'INSERT ADSENSE CODE HERE' twice" - possibly because of the OR in the conditional statement if ($post_counter == 2 || $post_counter == 3) ? or do you mean somthing else? – Michael Aug 7 '12 at 18:37
13

instead of a post counter, you could use the variable $wp_query->current_post - which starts with 0 for the first post in the loop;

for some output after the third post, use for instance this before the line with endwhile;:

<?php if( $wp_query->current_post == 2 ) { ?>
DO SOMETHING
<?php } ?>
  • do I need to put in singe post/page tempalte? if yes where exactly in it? – Vivek Sancheti Feb 20 '17 at 8:55
  • the code should go into the loop of the index template. detaills depend on your used theme. – Michael Feb 20 '17 at 17:55
  • Can we do this randomly in the loop ? – Muhammad Riyaz Jul 12 '17 at 10:14
  • @MuhammadRiyaz - generally yes, however, the code would be more complex because you need to get the random value before the loop and then use it instead of the number in the if statement. consider to start your own question to get a detailed answer. – Michael Jul 12 '17 at 16:11
  • @Michael - Thanks for reply. I found a solution. <?php $ad_positions = array(2,3,4,5,6); // array of preset possible ad positions $numb = $ad_positions[rand(0, count($ad_positions) - 1)]; if( $wp_query->current_post == $numb ) { ?> DO SOMETHING <?php } ?> – Muhammad Riyaz Jul 16 '17 at 6:55
2
<?php $ad_positions = array(2,3,4,5,6); // array of preset possible ad positions 
$numb = $ad_positions[rand(0, count($ad_positions) - 1)]; 

if( $wp_query->current_post == $numb ) { ?> 
DO SOMETHING 
<?php } ?>

If anybody need to show the ad in random positions, above code will help in the wordpress loop. Thank you.

protected by Community Oct 17 '17 at 15:59

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.