douzhanhui5662 2014-10-01 15:54
浏览 55

使用wordpress将图像和标题输出到bootstrap轮播

I am trying to use wordpress to add images to my bootstrap carousel, I have attempted a foreach loop and then try to output each part of the array i want into the correct parts of the carousel however it doesn't seem to work.

Here is the code i'm using

<?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>
  • 写回答

1条回答 默认 最新

  • dongsu7049 2014-10-01 17:02
    关注

    On this line you have an extra apostrophe at the beginning:

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

    It should read:

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

    To make:

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

    And here's a refined version using the php ternary operator and the shortcut <?= instead of <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元