简体   繁体   中英

How to insert a custom code between the posts inside the Wordpress loop?

Right now I have a very basic Wordpress loop:

<?php 

// get posts
$posts = get_posts(array(
    'post_type'         => 'page',
    'posts_per_page'    => 30,
    'order'             => 'DESC'
));

if( $posts ): ?>

<?php foreach( $posts as $post ): 
        
setup_postdata( $post )
        
        ?>
        
    
<?php the_title(); ?>
    

<?php endforeach; ?>
    
<?php wp_reset_postdata(); ?>

<?php endif; ?> 

Basically I'm trying to insert an ad between the posts.

And have an option to specify after how many posts the ad code would appear.

So if I type 3, then it would look like this:

Post 1 title

Post 2 title

Post 3 title

AD

Post 4 title

Post 5 title

Post 6 title

AD

...

In the other similar thread I have found that I can do that using a counter:

$i = 1;

if($i==3||$i==5||$i==10){
            /*Adsence here*/
            }

$i++;

But I have no idea how to implement that into my code.

Not a coder, just trying to combine something.

Thanks.

You can initialize a counter, set it to zero and add one to it after every loop. You can then check for the current value of the counter and do things when it reaches a certain value.

<?php 

// get posts
$posts = get_posts(array(
    'post_type'         => 'page',
    'posts_per_page'    => 30,
    'order'             => 'DESC'
));

$counter = 0;

if( $posts ): ?>

<?php foreach( $posts as $post ): 
        
setup_postdata( $post )     
?>
          
<?php the_title(); ?>

<?php 
// checks if $counter exists in the array
if (in_array($counter, array(3, 6, 9))) {
    // do something when $counter is equal to 3, 6, or 9
    // such as rendering an ad
}

$counter++;
?>

<?php endforeach; ?>
    
<?php wp_reset_postdata(); ?>

<?php endif; ?>  

You could set this up in multiple ways. For example you could use current_post inside the loop to get the current loop index:

$posts = new wp_query(
  array(
    'post_type'         => 'page',
    'posts_per_page'    => 30,
    'order'             => 'DESC'
  )
);

if( $posts ):

  while($posts->have_posts())
  {

    $posts->the_post();

    $current_post = $posts->current_post; // starts from zero

    if($current_post == 2 || $current_post == 4 || $current_post == 9):?>

      <div>YOUR ADSENCE</div>

    <?php

    endif;

    ?>

    <h3><?php the_title(); ?></h3>

  <?php 
  }

endif;

Or if you want to use a counter , then you could do something like this:

$posts = new wp_query(
  array(
    'post_type'         => 'page',
    'posts_per_page'    => 30,
    'order'             => 'DESC'
  )
);

if( $posts ):

  $i = 1; // starts from one OR you could change it to zero to starts from zero if you want to

  while($posts->have_posts())
  {

    $posts->the_post();

    if($i == 3 || $i == 5 || $i == 10):?>

      <div>YOUR ADSENCE</div>

    <?php

    endif;

    ?>

    <h3><?php the_title(); ?></h3>

  <?php 

  $i++;

  }

endif;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM