我正在尝试使用wordpress将图像添加到我的bootstrap轮播中,我尝试了一个foreach循环,然后尝试将我想要的数组的每个部分输出到旋转木马的正确部分但是它似乎并不像工作。
以下是我使用
的代码<?php
$args = array(
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => ASC,
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts($args);
$imageURL = array();
?>
<!-- Content -->
<div class="container">
<div class="row carousel-row">
<div class="col-md-12">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php $i=0; foreach ($attachments as $imageURL) { ?>
'<div class="item <?php if ($i == 0) { echo 'active'; } ?>" style="background-size:cover; background:url('<?php echo $imageURL[guid]; ?>') no-repeat center;">
<div class="carousel-caption">
<h4><?php echo $imageURL[post_excerpt];?></h4>
</div>
</div>
<?php $i++; } ?>
</div>
</div>
答案 0 :(得分:0)
在这一行,你在开头有一个额外的撇号:
'<div class="item <?php if ($i == 0)
应该是:
<div class="item <?php if ($i == 0)
制作:
<div class="carousel-inner">
<?php $i=0; foreach ($attachments as $imageURL) { ?>
<div class="item <?php if ($i == 0) { echo 'active'; } ?>" style="background-size:cover; background:url('<?php echo $imageURL[guid]; ?>') no-repeat center;">
<div class="carousel-caption">
<h4><?php echo $imageURL[post_excerpt];?></h4>
</div>
</div>
<?php $i++; } ?>
</div>
这是使用php ternary运算符和快捷键&lt;?= 而不是&lt;?php echo 的精炼版本:
<div class="carousel-inner">
<?php $i=0; foreach ($attachments as $imageURL) { ?>
<div class="item <?= ($i == 0 ? 'active' : '') ?>" style="background-size:cover; background:url('<?= $imageURL[guid] ?>') no-repeat center;">
<div class="carousel-caption">
<h4><?= $imageURL[post_excerpt] ?></h4>
</div>
</div>
<?php $i++; } ?>
</div>