0

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 } ?>
1
  • 2
    Use a temporary variable and reset it every three loops. Commented Apr 8, 2013 at 13:27

5 Answers 5

2
$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 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This seems the best out of all the answers! Works like a charm
1

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.

Comments

0
<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>

Comments

0

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>';

Comments

0

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 } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.