每个循环的备用布局

Alternate layout for every loop

在每个循环中我都想改变我的块的布局,所以它会是这样的:

循环 1:左栏图片,右栏文字 循环 2:左栏文字,右栏图片

但到目前为止我所管理的只是每个块都在同一侧或第一个块是重复的,这是我目前的代码:

任何帮助或正确方向的观点将不胜感激

<?php $i = 0;
foreach ($homepageblocks as $block):
$i++; ?>
<div class="row">
    <div class="container container-fluid">
        <div class="col-md-6">

                <h2><?php echo $block['title']; ?></h2>
                <?php echo $block['content']; ?>
                <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a>
        </div>
        <div class="col-md-6 nopadding">
            <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>"/>
        </div>
    </div>
</div>
<?php if($i%2 == 0): ?>
<div class="row">
    <div class="container container-fluid">
        <div class="col-md-6 nopadding">
            <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>" />
        </div>
        <div class="col-md-6">
            <h2><?php echo $block['title']; ?></h2>
            <?php echo $block['content']; ?>
            <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a>
        </div>
    </div>
</div>
<?php endif; ?>
<?php endforeach; ?>

else 分支添加到您的 if:

foreach ($homepageblocks as $block):
    $i++;?>
<div class="row">
    <div class="container container-fluid">
<?php
    // one layout type
    if($i%2 == 0):?>
        <div class="col-md-6 nopadding">
            <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>" />
        </div>
        <div class="col-md-6">
            <h2><?php echo $block['title']; ?></h2>
            <?php echo $block['content']; ?>
            <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a>
        </div>

<?php
    // another layout type
    else:?>
        <div class="col-md-6">
                <h2><?php echo $block['title']; ?></h2>
                <?php echo $block['content']; ?>
                <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a>
        </div>
        <div class="col-md-6 nopadding">
            <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>"/>
        </div>
<?php 
    endif;?>
    </div>
</div>
<?php 
endforeach; ?>