0

我有一個自定義帖子類型,可能包含或不包含要在輪播/幻燈片中顯示的圖像。如果已經通過高級自定義字段將圖像添加到帖子中,我該如何纔能有輪播顯示?以下是我到目前爲止的代碼。它的工作原理除了沒有與帖子相關的圖像之外,前端仍然顯示轉盤控件和其他東西。只有存在圖像時才顯示旋轉木馬。 (在Wordpress站點中使用高級自定義字段添加圖像的引導程序輪播)

要做到這一點,知道如何使指標自動填充所添加圖片的數量會很棒。截至目前,我已將指標評論出來。

<!-- slideshow --> 
<div id="myCarousel" class="carousel slide" data-ride="carousel"> 
    <!-- Indicators --> 
    <!-- <ol class="carousel-indicators"> 
     <li data-target="#myCarousel" data-slide-to="0" class="active"></li> 
     <li data-target="#myCarousel" data-slide-to="1"></li> 
     <li data-target="#myCarousel" data-slide-to="2"></li> 
     <li data-target="#myCarousel" data-slide-to="3"></li> 
    </ol> --> 

    <!-- Wrapper for slides --> 
    <div class="carousel-inner" role="listbox"> 
     <?php if(have_rows('timeline_images')): ?> 
      <?php while(have_rows('timeline_images')): the_row(); 
       // vars 
       $image = get_sub_field('timeline_image'); 
       ?> 

       <div class="item"> 
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" title="<?php echo $image['title'] ?>" /> 
       </div> 

      <?php endwhile; ?> 
     <?php endif; ?> 
    </div> 

    <!-- Left and right controls --> 
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"> 
     <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
     <span class="sr-only">Previous</span> 
    </a> 
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"> 
     <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
     <span class="sr-only">Next</span> 
    </a> 
</div> 
<!-- /slideshow --> 

回答

0

爲您與您的圖像(未測試)你也許可以做同樣的你的指標:

<div id="myCarousel" class="carousel slide" data-ride="carousel"> 
    <!-- Indicators --> 
    <ol class="carousel-indicators">  
     <?php if(have_rows('timeline_images')): ?> 
      <?php 
      $carrousselCount = 0; 
      while(have_rows('timeline_images')): the_row(); 
      $carrousselCount++; 
      ?> 
      // indicators 
      <li data-target="#myCarousel" data-slide-to="<?php echo $carrousselCount ?>" class="<?php echo $carrousselCount === 0 ? 'active' : '' ?>"></li> 
      <?php endwhile; ?> 
     <?php endif; ?> 
     <!-- end ol --> 
    </ol> 
    </div> 
+0

這似乎只是'數據載玻片到='等於工作每個訂單項都爲0。 – user2215732

+0

是的,我在while循環中忘了$ carrousselCount ++(現在更新) – Rienk

+0

這樣做了。我改變了'$ carrousselCount = 0;'爲'$ carrousselCount = -1;',因爲第一個數字需要爲0 – user2215732