2014-01-15 100 views
2

我在我的網站上使用ACF插件。 我想在一個頁面上顯示來自子頁面的中繼器字段的第一行(圖像URL)。獲得ACF中繼器字段的第一行(圖片)

這裏是網頁從我的網站(所有圖像都被加載,但只有第一個顯示的,但我想加載只有第一)

http://www.amsbooking.fr/mattieumoreau/artists/

這裏是我的網頁上的代碼這顯示所有圖像:

 <?php 

     $args = array(
     'post_type'  => 'page',  
     'post_parent' => $post->ID, 
     'order'   => 'ASC', 
     'orderby'  => 'menu_order' 
     ); 

     $parent = new WP_Query($args); 

     if ($parent->have_posts()) : ?> 

     <?php while ($parent->have_posts()) : $parent->the_post(); ?> 

     <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 

     <div class="cartouche_menu_artistes"> 

     <div id="parent-<?php the_ID(); ?>" class="parent-page"> 

     <div class="cartouche_crop"> 

     <?php while(has_sub_field('photos_presse')): ?> // my repeater field 

     <img src="<?php the_sub_field('photo'); ?>" class="artists_menu"> // my sub-field, the one I want to get the first row 

     <?php endwhile; ?> 


     </div> 

     <div class="bandeau"></div> 

     <h1><?php the_title(); ?></h1>    

     <div class="bandeau"></div> 

     </div> 


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


     <?php endif; wp_reset_query(); ?> 

是照片的URL,而不是照片本身...這就是爲什麼我惹上麻煩的第一行,但我想保持這種方式。

我發現這個在網絡上,但我不能管理,以使其適應我的情況:

<?php 

$rows = get_field('repeater_field_name'); // get all the rows 
$first_row = $rows[0]; // get the first row 
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value 

// Note 
// $first_row_image = 123 (image ID) 

$image = wp_get_attachment_image_src($first_row_image, 'full'); 
// url = $image[0]; 
// width = $image[1]; 
// height = $image[2]; 
?> 
<img src="<?php echo $image[0]; ?>" /> 

這裏是我的代碼的jsfiddle:

http://jsfiddle.net/5wtRc/

可以

人幫助我解決這個問題?

非常感謝您的幫助,

回答

2

我只是在當圖像被檢索其設置爲false,一個簡單的布爾補充說。然後我在布爾值中添加了while循環的條件,以便在一個圖像完成後,while循環將失敗並繼續前進!

<?php 
$first_img = true; 
while($first_img && has_sub_field('photos_presse')): ?> // my repeater field 

    <img src="<?php the_sub_field('photo'); ?>" class="artists_menu"> 
    <?php $first_img = false; ?> 

<?php endwhile; ?> 

希望這有助於!

+0

感謝您的回覆@Billy馬修斯,但它仍然加載所有的圖片...你可以請檢查源? http://www.amsbooking.fr/mattieumoreau/artists/ – mmdwc

+0

對不起,我不明白你的問題。你只想加載1個帖子? @ user2882154 – Bill

+0

不,我想從所有帖子的中繼器字段中加載第一張圖片。 – mmdwc

1

@ mmdwc是接近正確的。由於中繼器是嵌套字段,因此get_field不會檢索任何內容,因爲您嘗試檢索子字段。

<?php 
    $rows = get_sub_field('repeater_field_name'); // get all the rows of the sub field repeater 
    $first_row = $rows[0]; // get the first row of the repeater 
    $first_row_image = $first_row['sub_field_name' ]; // get the sub field value of the nested repeater 
?>