【问题标题】:Outputting images and captions into bootstrap carousel using wordpress使用 wordpress 将图像和标题输出到引导轮播
【发布时间】:2014-10-01 15:54:00
【问题描述】:

我正在尝试使用 wordpress 将图像添加到我的引导轮播,我尝试了一个 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>

【问题讨论】:

    标签: php jquery css wordpress twitter-bootstrap


    【解决方案1】:

    在这一行的开头有一个额外的撇号:

    '<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 运算符和快捷方式 = 而不是 的改进版本:

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

    【讨论】:

      猜你喜欢
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多