WP PHP - Foreach adding different classname - how does it work?
Add the index to your foreach loop:
foreach ($postslist as $index => $post) :
and use that to append to the class name:
<div class="testrun<?= $index ?>">
As long as the array is an indexed array, you'll get testrun0
, testrun1
, testrun2
etc.
If you want to start with 1
instead of 0
, just change it to:
<div class="testrun<?= $index + 1 ?>">
You can try something like this.
<?php
$i = 1;
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="testrun <?php echo "item".$i; ?>">
<img src="<?php the_post_thumbnail_url('blog-small');?>" alt="<?php the_title();?>" style="margin-right: 20px">
<br />
<!-- <?php the_date(); ?>
<br />
<?php the_title(); ?>
<br />
<?php the_excerpt(); ?>
<br /> -->
</div>
<?php $i++; endforeach; ?>