0

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

循环 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; ?>
4

1 回答 1

1

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; ?>
于 2017-09-26T18:08:04.663 回答