2013-07-23 47 views
0

完整的新手到PHP,但在拼湊在一起並不壞。我已經開始使用高級自定義字段的Wordpress,它非常有幫助。WordPress的條件滑塊(高級自定義字段)

我把代碼放在一起,把一個畫廊字段變成一個jQuery滑塊(你在後端的圖像中查找並循環並創建滑塊)。但是,我希望它只是在只輸入一個圖像的情況下加載靜態圖像。在psudo代碼中:

如果字段包含多個圖像,則通過滑塊代碼循環else(如果只有一個圖像),則將一個圖像包裹在img標記中。

當前實時代碼生成滑塊:

<?php 

/* Create the Markup for a slider */ 

$images = get_field('hero_image_gallery'); 

if($images): ?> 
    <div id="slider" class="flexslider"> 
     <ul class="slides"> 
      <?php foreach($images as $image): ?> 
       <li> 
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> 
        <p class="flex-caption"><?php echo $image['caption']; ?></p> 
       </li> 
      <?php endforeach; ?> 
     </ul> 
    </div> 
<?php endif; 

?> 

任何想法?最初我以爲我想要計數$圖像,但我不確定是否包含圖庫中的圖像數量。

編輯:只是把外面開始hero_image_gallery:

陣列(2)

目前旗下有2個圖像在那裏,如果我添加了三分之一,我得到陣列(3)。

編輯2:完整轉儲下面的hero_image_gallery。我不得不編輯一些細節,因爲我只能作爲新手發佈1個URL。 (2){[0] =>數組(9){[「id」] => int(971)[「alt」] =>字符串(14)「School」[「title」] = > string(12)「home-image-2」[「caption」] => string(0)「」[「description」] => string(0)「」[「url」] => string(74)「 #「[」width「] => int(1260)[」height「] => int(860)[」sizes「] => array(9){[」thumbnail「] => string(74) [「thumbnail-width」] => int(150)[「thumbnail-height」] => int(102)[「medium」] => string(74)「#」[「medium-width」] => int (300)[「medium-height」] => int(204)[「large」] => string(74)「#」[「large-width」] => int(1024)[「large-height」] => int(698)}} [1] => array(9){[「id」] => int(977)[「alt」] => string(0)「」[「title」] => string (12)「home-image-1」[「caption」] => string(0)「」[「description」] => string(0)「」[「url」] => string(74)「#」 [「width」] => int(1260)[「height」] => int(860)[「sizes」] => array(9){[「thumbnail」] => string(74) thumbnail-width「] => int(150)[」thumbnail-height「] => int(102)[」medium 「] => string(74)」#「[」medium-width「] => int(300)[」medium-height「] => int(204)[」large「] => string(74) 「[」large-width「] => int(1024)[」large-height「] => int(698)}}}

回答

0

我想你可能已經回答了你自己的問題!如果您目前有2個圖像,當您查看返回的數組,並與2分的結果回來,那就要檢查一下,如果你只有1幅圖像,你會做這樣的事情:

if(count($images) == 1) 

編輯:加滿代碼

<?php 

/* Create the Markup for a slider */ 

$images = get_field('hero_image_gallery'); 

if($images && count($images) == 1) { ?> 
    <!-- there is only 1 image --> 
<?php } else if ($images && count($images) > 1) { ?> 
    <!-- there is more than 1 image --> 
    <div id="slider" class="flexslider"> 
     <ul class="slides"> 
      <?php foreach($images as $image): ?> 
       <li> 
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> 
        <p class="flex-caption"><?php echo $image['caption']; ?></p> 
       </li> 
      <?php endforeach; ?> 
     </ul> 
    </div> 
<?php } ?> 
+0

感謝您的回覆。我的麻煩是我完全理解了這些概念,但我還沒有學會正確編寫PHP代碼(我是一名設計師,讓我的手變得「髒」)。 – lotech

+0

你會到達那裏!我覺得學習語言的最好方法是通過反覆試驗。只要邏輯思考 - 當你添加第三個圖像時,它會向數組添加一些東西。所以你可以推斷(並通過一些測試來驗證)1圖像只返回1個數組中的結果。 – zachwills

+0

感謝您的支持。我確實得到了那部分:p 我的意思是我不確定如何將它寫入一個完整有效的PHP塊! – lotech

相關問題