繁体   English   中英

使用Wordpress将图像和标题输出到引导轮播

[英]Outputting images and captions into bootstrap carousel using wordpress

我正在尝试使用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>

在此行的开头,您要加上一个撇号:

'<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 三元运算符和快捷方式<?=而不是<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM