• Resolved daniellefrappier

    (@daniellefrappier)


    I know I’m doing something wrong here but I can figure out how to order this loop. I’m trying to track a slideshow and I’m using counter to output what slide got clicked. Can anyone point me in the right direction?

    <ul class="slides">
               <?php $counter = 0;
                <?php foreach( $images as $image ): ?>
                $i++;
                <a href="#">', '<?php echo '$counter'; ?>');">
    
    <li>
                        <img src="<?php echo $image['sizes']['small-thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
                    </li>
                </a>
                $counter++;
                <?php endforeach; ?>
                ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    So what’s the issue>

    Thread Starter daniellefrappier

    (@daniellefrappier)

    It generates an syntax error. I also tried:

    <ul class="slides">
               <?php $counter = 0;
                foreach( $images as $image ) {
                $i++;
                <a href="#">
    
    <li>
                        <img src="echo $image['sizes']['small-thumbnail'];" alt="echo $image['alt'];" />
                    </li>
                </a>
                $counter++;
                }
                ?>

    Thread Starter daniellefrappier

    (@daniellefrappier)

    It generates a syntax error. I edited to this which i think is closer but i’m still getting synatx errors.

    <ul class="slides">
               <?php $counter = 0;
                foreach( $images as $image ) {
                $i++;
                <a href="#" onClick="ga('send', 'event', 'Slideshow', 'slideshowclick', '<?php the_title(); ?>', '<?php echo $counter; ?>');">
                    <li>
                        <img src="<?php echo $image['sizes']['small-thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
                    </li>
                </a>
                $counter++;
                }
                ?>
            </ul>
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What is the syntax error?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You need to ensure that you close off PHP before you write HTML, e.g.:

    <p>
    <?php
     foo = 1;
    ?>
    
    <a> ... </a>
    
    <?php
     bar = 0;
    ?>
    </p>

    Thread Starter daniellefrappier

    (@daniellefrappier)

    thanks that really helped. I got it working with this:

    <ul class=”slides”>
    <?php $count = ‘1’; ?>
    <?php foreach( $images as $image ): ?>
    ‘, <?php echo $count; ?>);”>

    • <img src=”<?php echo $image[‘sizes’][‘small-thumbnail’]; ?>” alt=”<?php echo $image[‘alt’]; ?>” />
    • <?php $count++; ?>
      <?php endforeach; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Counter inside a foreach loop’ is closed to new replies.