• ok, I’ll do my best to explain what i need. I am using this loop to get the image paths and then I build the div tags and links.


    <?php wp_reset_query(); ?>

    <?php query_posts(‘category_name=Photos&posts_per_page=6’); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php $images = get_post_meta($post->ID, ‘coverthumb’, false); ?>

    <?php
    foreach($images as $image) {
    echo ‘<div id=”photosWrapper”>’;
    echo ‘<div id=”photos”>’;
    echo ‘<a href=”‘;
    echo ”.the_permalink().'”>’;
    echo ‘<img src=’.$image.’.jpg rel=”shadowbox” border=”0″ class=”a”/>’;
    echo ”;
    echo ‘</div>’;
    echo ‘</div>’;
    }
    ?>

    <?php endwhile; ?>

    <?php endif; ?>

    I want to be able to put in a counter (e.g. $i=0; i++;) in the foreach loop so that I can use different styles depending on which image is being loaded. Basically, I want every 3rd image to have a different id.

    Any help would be greatly appreciated. I’m pretty sure it has something to do with adding => $value to the end of the foreach loop but there is nothing to put in there so that just kicks back an error.

Viewing 6 replies - 1 through 6 (of 6 total)
  • $counter = 0;
    foreach($images as $image) {
    echo '<div id="photosWrapper">' . '<div id="photos">' . "\n" . '<a href="' . the_permalink() . '<img src='.$image.'.jpg rel="shadowbox" border="0" class="a"/></div></div>';
    
    $counter++;
    
    if( $counter >= 3 ) {
    // do something here
    $counter = 0; // reset your counter
    }
    }

    A programming tip: Use less echo’s, the example I used above concatenates the strings but instead of using the dot you can use a comma or use “here document” syntax.

    Eg:

    echo <<<END
    This uses the "here document" syntax to output
    multiple lines with $variable interpolation. Note
    that the here document terminator must appear on a
    line with just a semicolon. no extra whitespace!
    END;

    Thread Starter blitzed2010

    (@blitzed2010)

    Hey bizzylabs thanks for the reply, but no dice. The counter doesn’t increment. That’s the problem I’ve been running into.

    Thread Starter blitzed2010

    (@blitzed2010)

    here’s what i did based on your suggestions

    <?php $counter = 0;

    foreach($images as $image) {

    echo ‘<div id=”photosWrapper”>’.”\n”.'<div id=”photos”>’.”\n”;
    echo ‘<a href=”‘;
    echo ”.the_permalink().'”>’;
    echo ‘<img src=’.$image.’.jpg rel=”shadowbox” border=”0″ class=”a”/>’.”\n”.'</div>’.”\n”.'</div>’;
    echo $counter;
    $counter++;

    }
    ?>

    This returns all Zero’s as the value for the $counter var. Ideas?

    Thread Starter blitzed2010

    (@blitzed2010)

    here’s the updated code so that it’s only using two echo’s as suggested. The counter is still not incrementing. Is this even possible to do? Is it b/c it’s using a wordpress function (get_post_meta)?


    <?php $images = get_post_meta($post->ID, ‘coverthumb’, false); ?>

    <?php $counter = 0;

    foreach($images as $image) {

    echo ‘<div id=”photosWrapper”>’,”\n”,'<div id=”photos”>’,”\n”,’‘,'<img src=’,$image,’.jpg rel=”shadowbox” border=”0″ class=”a”/>‘,”\n”,'</div>’,”\n”,'</div>’;
    echo $counter;
    $counter++;

    }
    ?>

    If it is printing out only one zero then there is only one image in the loop to go through, if it is printing out more then one zero then try using a for loop instead and see what happens.

    Example:

    for( $i=0; $i < count( $images ); $i++ ) {
      echo '<div id="photosWrapper"><div id="photos"><img src=' . $images[$i] . '.jpg rel="shadowbox" border="0" class="a"/></div></div>';
    }
    Thread Starter blitzed2010

    (@blitzed2010)

    Solved! Thanks bizzylabs for the advice. The solution was not to declare the $counter var. Just in case anyone else is looking for it here is what I ended up with.


    <?php

    foreach($images as $image) {
    if ($counter == 2 || $counter == 5) {
    echo ‘<div id=”photosLast”>’,”\n”,’‘,'<img src=’,$image,’.jpg rel=”shadowbox” border=”0″ class=”a”/>‘,”\n”,'</div>’;
    }else{
    echo ‘<div id=”photos”>’,”\n”,’‘,'<img src=’,$image,’.jpg rel=”shadowbox” border=”0″ class=”a”/>‘,”\n”,'</div>’;
    }

    $counter++;
    }
    ?>

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