0

In my wordpress home.php file, I have placed an increment loop with the help of a few stackexchange threads:

<?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
        <?php $count++; ?>
    <?php if ($count == 3) : ?>
        <?php if (function_exists ('adinserter')) echo adinserter (1); ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
        <?php $count = 0; ?>    
    <?php else : ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
    <?php endif; ?>
 <?php endwhile; ?>
<?php endif; ?>

Where, inside the content.php file, I have the following:

<div class="row">
    <div class="col-md-3">
            <?php
            if ( has_post_thumbnail()) {
                echo '<a href="' . get_permalink($post->ID) . '" >';
                the_post_thumbnail('my_feature_image', array( 'class' => "img-responsive" ));
                echo '</a>';
            }
            ?>
    </div>
    <div class="col-md-9">
            <h3 class="article-list-header"><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong></h3>
            <h4 class="article-list-subheader"><?php the_time('l, F jS, Y'); ?></h4>
            <p><?php the_content(); ?></p>
    </div>
</div>

As you can see above, I have tried to place this: <?php if (function_exists ('adinserter')) echo adinserter (1); ?> after every three posts which doesn't seem to work no matter what value I add to the count. It echo the php function at random positions and it also doesn't seem to work on the next page of my infinite scroll homepage.

Can I please get some pointers on what I'm doing wrong here.

2
  • 1
    How and where does the the shortcode comes in. Seeing no shortcode anywhere Commented Jul 8, 2015 at 4:26
  • This one Pieter: <?php if (function_exists ('adinserter')) echo adinserter (1); ?>
    – AndrewL64
    Commented Jul 8, 2015 at 9:29

1 Answer 1

2

First, let's clean up this tag SPAM nightmare so we can read it and then simplify the code:

function adinserter() {
  return 'abcdefg';
}

if (have_posts()) { 
  $count = 0; 
  while (have_posts()) { 
    the_post(); 
    $count++; 
    get_template_part( 'content', get_post_format() ); 
    if ($count == 3) { 
      if (function_exists ('adinserter')) {
        echo adinserter (1); 
      }
      $count = 0; 
    }
  } 
}

You were duplicating code by having get_template_part() inside and else conditional and also inside the if itself. That code runs all the time. It doesn't need to be in a conditional at all.

Next, your code works except for the placement of the echo function. Placing that before get_template_part() made the first add come out between posts #2 and #3-- which is what I assume you mean by the code being inserted "at random positions". The rest dropped in correctly. The way you had the if/else written this may have been hard to spot, but as soon as I cleaned that up the answer was fairly obvious.

So far, not much WordPress to it. The WordPress is this: you don't need the counter. There is one built into the Loop. It is provided for you.

if (have_posts()) { 
  while (have_posts()) { 
    the_post(); 
    get_template_part( 'content', get_post_format() ); 
    if ($wp_query->current_post !== 0 && ($wp_query->current_post+1)%3 == 0) { 
      if (function_exists ('adinserter')) {
        echo adinserter (1); 
      }
    }
  } 
}

As far as why it doesn't work with your infinite scroll, I can't say as I don't know how your infinite scroll works.

10
  • Really clear explanation! Thank you good sir! But how do I recall/reinitiate the count++ for the next page too? The infinite scroll I'm using basically shows 7 posts a page and shows another 7 posts on repeat till there's no post left.
    – AndrewL64
    Commented Jul 7, 2015 at 23:53
  • This is the one I'm using: jetpack.me/support/infinite-scroll
    – AndrewL64
    Commented Jul 7, 2015 at 23:55
  • I wouldn't mind inserting the short code only once per page too, like after the third or fourth post of each page
    – AndrewL64
    Commented Jul 7, 2015 at 23:56
  • Having 7 posts is going to throw off the internal logic. You will probably need your own counter and possibly need to pass a value to the Javascript that powers the infinite scroll, but again, without having that code...
    – s_ha_dum
    Commented Jul 7, 2015 at 23:58
  • Just use if ($wp_query->current_post !== 3) to insert the code once.
    – s_ha_dum
    Commented Jul 7, 2015 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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