1

我正在尝试使用 ACF 的中继器字段显示带有图像的 div 列表。

这是我正在使用的代码:

<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php 
$slides = get_field('project_thumbnails');  // Grabs the array

    // Check if there is any data in the array before looping
    if($slides) {
    echo '<div id="project_slider" class="item">';
    foreach($slides as $s) {
        echo '<a href="#">';
        echo '<img src="'.$s['project_thumb'].'" alt="" />';
        echo '</a>';
    }
       echo '<div class="art_title">';
       echo '<p>SWEET LIFE2</p>';
       echo '</div>';
       echo '<div class="mask">';
       echo '</div>';
    }

?>

<?php endwhile; // end of the loop. ?>

这样做的问题是它在同一个 div 中显示所有图像,而不是在新 div 中显示转发器字段的每个图像。

我做错了什么?

4

1 回答 1

3

我不是 php 专家,但我认为这只是缺少第一个 div 的结束标签的问题,我也要把另一个 div 放在循环中,它看起来像这样:

        <?php  $slides = get_field('project_thumbnails');  
            // Grabs the array      
           // Check if there is any data in the array before looping
             if($slides) {     
                //we need to close this div
               echo '<div id="project_slider" class="item">';     
               foreach($slides as $s) {  
                      echo '<div class="aimagediv" >'; //adding the start tag of the div
                      echo '<a href="#">';          
                      echo '<img src="'.$s['project_thumb'].'" alt="" />';
                      echo '</a>';
                      echo '</div>'; //CLOSING THE DIV JUST ADDED     
                     }        
                      echo '<div class="art_title">';        
                      echo '<p>SWEET LIFE2</p>';        
                      echo '</div>';        
                      echo '<div class="mask">';        
                      echo '</div>';  

                      echo '</div>'; //closing the first div,not sure if you want this, its optional
                 }  

    ?>  <?php endwhile; // end of the loop. ?> 

另一种选择是这样(只是弄清楚您期望的结果布局是什么):

<?php  $slides = get_field('project_thumbnails');  
                // Grabs the array      
               // Check if there is any data in the array before looping
                 if($slides) {     
                    //we need to close this div

                   foreach($slides as $s) {  
                          echo '<div id="project_slider" class="item">';  
                          echo '<a href="#">';          
                          echo '<img src="'.$s['project_thumb'].'" alt="" />';
                          echo '</a>';
                          echo '</div>'; //CLOSING THE DIV JUST ADDED     
                         }        
                          echo '<div class="art_title">';        
                          echo '<p>SWEET LIFE2</p>';        
                          echo '</div>';        
                          echo '<div class="mask">';        
                          echo '</div>';  
                     }  

        ?>  <?php endwhile; // end of the loop. ?> 

希望这已经足够了。

于 2012-10-16T18:20:13.647 回答