Rik89
Rik89

Reputation: 135

Use foreach loop with counter

I want to output the results of a foreach statement but I want them grouped into a div in 3's

So like:

<div>image image image</div>
<div>image image image</div>
<div>image image image</div>

Here is my code so far:

$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );

            $myrows = get_posts($args);
                foreach($myrows as $row) {  ?>
                <div>
                    <?php if ( has_post_thumbnail($row->ID)) {
                    echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
                        echo get_the_post_thumbnail($row->ID);
                    echo '</a>';
                    }?>
                </div>
                <?php } ?>

Upvotes: 0

Views: 205

Answers (5)

Yuvraj Jain
Yuvraj Jain

Reputation: 193

Try this code.

<?php
$args = array( 'numberposts' => 3, 'offset'=> 0, 'category' => 9 );

            $myrows = get_posts($args);
            $tempCnt=0;
                foreach($myrows as $row) {
                    //12
                    if($tempCnt==3)
                    {
                        $tempCnt=0;
                        //do your reset code here.
                    }
                    $tempCnt++;
                    ?>
                <div>
                    <?php if ( has_post_thumbnail($row->ID)) {
                    echo '<a href="' . get_permalink( $row->ID ) . '" title="' . esc_attr($row->post_title ) . '">';
                        echo get_the_post_thumbnail($row->ID);
                    echo '</a>';
                    }?>
                </div>
                <?php } ?>

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157839

$myrows = get_posts($args);
$chunks = array_chunk($myrows,3);
?>
<?php foreach($chunks as $myrows): ?>
<div>
    <?php foreach($myrows as $row): ?>
    <div>
        <?php if(has_post_thumbnail($row->ID)): ?>
        <a href="<?=get_permalink($row->ID)?>" title="<?=esc_attr($row->post_title)?>">
            <?=get_the_post_thumbnail($row->ID)?>
        </a>
        <?php endif ?>
    </div>
    <?php endforeach ?>
</div>
<?php endforeach ?>

Upvotes: 2

haim770
haim770

Reputation: 49095

Why not use the modulo operator?

$counter = 0;

echo '<div>';

foreach ($myrows as $row)
{
    $counter++;

    if ($counter % 3 == 0) echo '</div><div>';

    echo $row;
}

echo '</div>';

Upvotes: 0

dikirill
dikirill

Reputation: 1903

<div>
    <?php
    $args = array('numberposts' => 3, 'offset' => 0, 'category' => 9);

    $myrows = get_posts($args);
    foreach($myrows as $idx => $row) {
    if ($idx % 3 == 0) echo "</div><div>";
    if (has_post_thumbnail($row->ID)) {
        echo '<a href="' . get_permalink($row->ID) . '" title="' . esc_attr($row->post_title) . '">';
        echo get_the_post_thumbnail($row->ID);
        echo '</a>';
    } ?>
</div>

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173552

You can create blocks using array_chunk():

foreach (array_chunk($myrows) as $mychunk) {
    echo '<div>';
    foreach ($mychunk as $row) {
        // print your entries
        if (has_post_thumbnail($row->ID)) {
            echo sprintf('<a href="%s" title="%s">%s</a>', 
                get_permalink( $row->ID ),
                esc_attr($row->post_title ),
                get_the_post_thumbnail($row->ID)
            );
        }
    }
    echo '</div>';
}

Granted, if the if condition isn't met, you would get blocks of zero, one or two items instead of the expected three.

Upvotes: 1

Related Questions