• Resolved twentyfourdegrees

    (@twentyfourdegrees)


    Hello everyone,

    I’m having a strange issue with tooltips I want to add to to a Bootstrap Carousel I’ve translated to PHP. Thing is, while in a loop, I want to add a tooltip of every post to the Carousel Indicators. I use the following code for that:

    <?php for($i = 0; $i < $count ;  $i++) { ?>
    <li data-target="#introCarousel" data-slide-to="<?php echo $i; ?>" class="text-center <?php echo ($i == 0) ? 'active' : ''?>" data-toggle="tooltip" title="<?php echo get_the_title(); ?>">
                                        <?php echo $i + 1; ?>
                                    </li>
                                <?php } ?>

    Even when I change:

    title="<?php echo get_the_title(); ?>"

    to

    title="<?php echo get_the_title($i); ?>"

    it always repeats the code of the first post found. Whatever I try, it’s always repeating the title from the first post found.

    What am I overlooking?

    Thanks for you help.

    kind regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You increment $i, but don’t load another post. And $i is just a counter, not the post ID, so you can’t use it in get_the_title.

    Thread Starter twentyfourdegrees

    (@twentyfourdegrees)

    Oh that’s true Joy, thanks for finding the issue !

    So this means my whole structure is a bit faulty?

    Thread Starter twentyfourdegrees

    (@twentyfourdegrees)

    I’m afraid that, only for the purpose of making the tooltips work, I have to create 2 loops, right?

    Thread Starter twentyfourdegrees

    (@twentyfourdegrees)

    Thanks, I get it now. FYI: I replaced the first loop with:

    <?php $i = 0; while($slider->have_posts()): $slider->the_post(); ?>

    and also added an endwhile of course.

    Thanks Joy!!

    Thread Starter twentyfourdegrees

    (@twentyfourdegrees)

    Resolved.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Getting post titles while in a loop’ is closed to new replies.