2014-10-01 42 views
0

我想使用wordpress将图像添加到我的引导程序传送带,我尝试了一个foreach循环,然后尝试输出数组的每个部分我想进入正确的部分旋转木马,但它似乎并没有工作。使用wordpress输出图像和标题到bootstrap传送带

这里是我使用

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

在此行中的代码,你有一个额外的撇号开头:

'<div class="item <?php if ($i == 0) 

它应该阅读:

<div class="item <?php if ($i == 0) 

要品牌:

??0

下面是使用PHP ternary运营商和<快捷< =,而不是改良版本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>