$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Echo a shortcode div after every 3 posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Echo a shortcode div after every 3 posts

matteradmin11PV0评论

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.

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.

Share Improve this question edited Jan 20, 2019 at 23:53 AndrewL64 asked Jul 7, 2015 at 23:17 AndrewL64AndrewL64 2034 silver badges18 bronze badges 2
  • 1 How and where does the the shortcode comes in. Seeing no shortcode anywhere – Pieter Goosen 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
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far