Can I add a different number for each <li> created in a for?

Asked

Viewed 66 times

3

I have a carousel in bootstrap and to call the wordpress posts inside it, I have a for within the <div class="carousel-inner">.

The for can create the posts, but I’m not able to include the indicators of each post (the balls that are below the carousel or <ol class="carousel-indicators">).

The structure of the indicators is:

<ol class="carousel-indicators">
   <li data-target="#id_carousel" data-slide-to="0"></li>
</ol>

What I need is this data-slide-to, as it is being set to 0 for each post that was created, ie regardless of which I click, the carousel back to first post.

What do I have:

<ol class="carousel-indicators">
   <?php for ($i = 1; $i <= NUMBER_OF_SLIDES; $i++) : ?>
      <li data-target="#carousel" data-slide-to="0" class="<?php echo ($i == 1) ? 'active' : ''; ?>"></li>
   <?php endfor; ?>
</ol>

As <li> are created within the for, but I’m not managing to assign a number other than data-slide-to for each <li> maid.

What I’m trying to do, is that the output of this for get out that way:

<li data-slide-to="0">
<li data-slide-to="1">
<li data-slide-to="2">
...

1 answer

4


Would that be?

<?php for ($i = 0; $i <= NUMBER_OF_SLIDES; $i++) : ?>
      <li data-target="#carousel" data-slide-to="<?php echo $i; ?>" class="<?php echo ($i == 1) ? 'active' : ''; ?>"></li>
<?php endfor; ?>
  • I tried this method earlier, but the problem is that by clicking any Bullet, it skips a slide or it won’t, even if it is 1 to 5 correctly. It will be because it is not starting at 0?

  • Well, that’s right, the lack to start at 0 hahahahaha I broke my head at that but thanks for the answer!

  • yes, probably. That’s why I started $i at 0. I think that should be it.

  • blz isso ae, show! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.