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 に答える