0

我想输出 foreach 语句的结果,但我希望将它们分组到 3 中的 div 中

就像:

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

到目前为止,这是我的代码:

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

5 回答 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 ?>
于 2013-04-08T13:31:14.687 回答
1

您可以使用以下方法创建块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>';
}

当然,如果if不满足条件,您将获得零块、一个或两个项目,而不是预期的三个。

于 2013-04-08T13:30:03.043 回答
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>
于 2013-04-08T13:31:31.470 回答
0

为什么不使用modulo运算符?

$counter = 0;

echo '<div>';

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

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

    echo $row;
}

echo '</div>';
于 2013-04-08T13:32:53.867 回答
0

试试这个代码。

<?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 } ?>
于 2013-04-08T13:34:10.733 回答