Support

Account

Home Forums Add-ons Repeater Field Repeater field split in 2 columns

Solved

Repeater field split in 2 columns

    • tiwelle

    • July 15, 2013 at 11:38 pm

    Hello,
    I’m trying to split my repeater field in 2 columns contained in 2 divs. Code is :

         
    <?php 
     $i = 0;
    $rows = get_field('articles_de_presse');
    $string = get_sub_field('contenu_de_larticle');
    echo '<div class="odd-column">' ;
    foreach($rows as $row) {
    $string = $row['contenu_de_larticle'];
    $auteurfield = $row['auteur_de_larticle'];
    $titrefield = $row['titre_du_support'];
       $compteur = strlen($string);
    if (($i++ % 2) == 0 ) { 
        if  ( $compteur < 220 ) { 
     
     echo '<div class="actupost"><h3 class="actu-title"><img src="http://www.editionsdusonneur.com/wp-content/themes/sonneur/img/bullet.gif">' . $row['auteur_de_larticle']  ;
     	 if( $auteurfield !="" AND  $titrefield !=""): ?>,
    				<?php endif;
      echo' <em>' . $row['titre_du_support'] .'</em></h3><div class="actuexeprt">'. $row['contenu_de_larticle'] .' </div><div class="actuinfo">
       '. $row['contenu_de_larticle'] .'</div></div>';
            }
    		else {
    			$small = substr($string,0,strpos($string,' ',220)); 
    
    echo '<div class="actupost"><h3 class="actu-title"><img src="http://www.editionsdusonneur.com/wp-content/themes/sonneur/img/bullet.gif">' . $row['auteur_de_larticle'] ;
    	 if( $auteurfield !="" AND  $titrefield !=""): ?>,
    				<?php endif;
      echo ' <em>' . $row['titre_du_support'] .'</em> <i class="icon-angle-down" title="Cliquez ici pour lire l\'article"></i></h3>  <div class="actuexeprt">'. $small .'...</em></b></strong></i> </div><div class="actuinfo">
       '. $row['contenu_de_larticle'] .'</div></div>';}    
    	   }}
    	   echo '</div>' ;
    	   
    	echo '<div class="even-column">' ;
    
    foreach($rows as $row) {
    $string = $row['contenu_de_larticle'];
    $auteurfield = $row['auteur_de_larticle'];
    $titrefield = $row['titre_du_support'];
    $compteur = strlen($string);
    
    if (($i++ % 2) == 0 ) { 
        if  ( $compteur < 220 ) { 
       
     echo '<div class="actupost"><h3 class="actu-title"><img src="http://www.editionsdusonneur.com/wp-content/themes/sonneur/img/bullet.gif">' . $row['auteur_de_larticle']  ;
     	 if( $auteurfield !="" AND  $titrefield !=""): ?>,
    				<?php endif;
      echo' <em>' . $row['titre_du_support'] .'</em></h3><div class="actuexeprt">'. $row['contenu_de_larticle'] .' </div><div class="actuinfo">
       '. $row['contenu_de_larticle'] .'</div></div>';
            }
    		else {
    			$small = substr($string,0,strpos($string,' ',220)); 
    
    echo '<div class="actupost"><h3 class="actu-title"><img src="http://www.editionsdusonneur.com/wp-content/themes/sonneur/img/bullet.gif">' . $row['auteur_de_larticle'] ;
    	 if( $auteurfield !="" AND  $titrefield !=""): ?>,
    				<?php endif;
      echo ' <em>' . $row['titre_du_support'] .'</em> <i class="icon-angle-down" title="Cliquez ici pour lire l\'article"></i></h3>  <div class="actuexeprt">'. $small .'... </div><div class="actuinfo">
       '. $row['contenu_de_larticle'] .'</div></div>';}    
    	   }}
    	   echo '</div>' ;?>   

    As you can see, it’s a conditionnal display, depending on the lenght of one field (to have it toggled when needed).
    When the number of field is odd, it works without problem. When number is odd, it stops working and repeat itself.
    Example : odd number http://www.editionsdusonneur.com/livre/la-tentation-de-la-bicyclette-dedmondo-de-amicis/
    even number : http://www.editionsdusonneur.com/livre/singapour-de-georges-cassel/

    • tiwelle

    • July 15, 2013 at 11:59 pm

    I figured it out

    Code is wrong on 2nd part.
    if (($i++ % 2) == 0 ) should be if (($i++ % 2) != 0 )

    And one line was missing in the middle $i = 0; rewind_posts();

    So in the end, general code is :

    <?php 
    $i = 0;
    $rows = get_field('articles_de_presse');
    	echo '<div class="odd-column">' ;
    		foreach($rows as $row) {
    			if (($i++ % 2) == 0 ) { 
     				echo '<div class="actupost"><h3>' . $row['auteur_de_larticle']  . '</h3><div class="actuexeprt">'. $row['contenu_de_larticle'] .' </div></div>';
    			}
    		}
    	echo '</div>' ;
    $i = 0; rewind_posts();
    $rows = get_field('articles_de_presse');
    	echo '<div class="even-column">' ;
    		foreach($rows as $row) {
    			if (($i++ % 2) != 0 ) { 
    				echo '<div class="actupost"><h3>' . $row['auteur_de_larticle']  . '</h3><div class="actuexeprt">'. $row['contenu_de_larticle'] .' </div></div>'; 
    			}
    		}
     	echo '</div>' ;?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.