Support

Account

Home Forums Add-ons Repeater Field Dynamic Repeater Fields with Titles

Solved

Dynamic Repeater Fields with Titles

  • So I have a repeater field for two separate pages. The idea is to cut down on code but so far I’m bloating all over the place!!

    It’s a list of sponsors, on one page its: partner, sponsor, in kind; and the other page: major and other.

    The fields are website, partner and image.

    What I’d like to be able to do is the following:

    Heading: Partner
    All fields marked as partner

    Heading: Sponsor
    All fields marked as sponsor

    But only show if there is a field present with the partner type.

    So if you go to a page it’ll check if it has fields, then it’ll display those fields in their groups, with a heading. Does that make sense?

    I can get them to show normally… but not with a heading and at the moment they’re all in the same div[row] and they should be in their own.

    I feel like there is a way I could do this with arrays but I’m a beginner.

    I can do it if I split them all up but it’s a heap of code.

    This is what I had:

    <?php if( have_rows('logos') ):  ?>
    	
    	<div class="row mb-5">
    	
    	<?php while( have_rows('logos') ): the_row();  ?>
    		
    <h3>Title would go Here</h3>
    		<?php if (get_sub_field('partner') == 'Major Project Sponsors') : ?>
    			<div class="col-sm-6 col-md-3 text-center py-3">
    				<a href="<?php the_sub_field('website'); ?>"><img src="<?php the_sub_field('logo'); ?>" /></a>
    			</div>
    		<?php endif; ?>
    
    <h3>Title would go Here</h3>
    		<?php if (get_sub_field('partner') == 'Other Project Sponsors') : ?>
    			<div class="col-sm-6 col-md-3 text-center py-3">
    				<a href="<?php the_sub_field('website'); ?>"><img src="<?php the_sub_field('logo'); ?>" /></a>
    			</div>
    		<?php endif; ?>
    
    <h3>Title would go Here</h3>
    		<?php if (get_sub_field('partner') == 'Partners') : ?>
    			<div class="col-sm-6 col-md-3 text-center py-3">
    				<a href="<?php the_sub_field('website'); ?>"><img src="<?php the_sub_field('logo'); ?>" /></a>
    			</div>
    		<?php endif; ?>
    
    <h3>Title would go Here</h3>
    		<?php if (get_sub_field('partner') == 'Sponsors') : ?>
    			<div class="col-sm-6 col-md-3 text-center py-3">
    				<a href="<?php the_sub_field('website'); ?>"><img src="<?php the_sub_field('logo'); ?>" /></a>
    			</div>
    		<?php endif; ?>
    
    <h3>Title would go Here</h3>
    		<?php if (get_sub_field('partner') == 'In Kind') : ?>
    			<div class="col-sm-6 col-md-3 text-center py-3">
    				<a href="<?php the_sub_field('website'); ?>"><img src="<?php the_sub_field('logo'); ?>" /></a>
    			</div>
    		<?php endif; ?>
    	
    	<?php endwhile; ?>
    	</div>
    <?php endif; ?>
  • Okay I’ve refined it a heap after looking into arrays:

    <?php if( have_rows('logos') ):
    
    	while( have_rows('logos') ): the_row(); 
    
    		$select = get_sub_field_object('partner');
    		$value = get_sub_field('partner');
    		$partnerArray = $select['choices']; 
    
    	endwhile;
    
     endif; ?>
    		
    <?php $colors = $partnerArray;
    	foreach ($colors as $color) {
    	    global $b;
    		$b++;
    			
    		echo "<h3>" . ucfirst($color) . "</h3>";
    		echo '<div class="row">';
    		while ( have_rows('logos') ) : the_row();
    		    if(get_sub_field('partner') == "{$color}"):
    				// display the repeater sub field values
    				echo    '<div class="col-sm-6 col-md-3 text-center py-3" id="'.$b.'">
    						 <a href="' . get_sub_field('website') .'"><img src="' .get_sub_field('logo') . '" /></a>
    					     </div>';
    			endif;
    		endwhile;
    	echo '</div>'; 
    }?>

    However it will display the titles regardless. If the titles exist, they are shown, even if there are no items on that page. I need them to only show if there are items.

  • I got to it in the end….

    Here’s my working solution:

    <?php 
    $all_attributes = array();
    $filter_attributes = array();
    
    if (have_rows('logos')): while (have_rows('logos')) : the_row(); 
        $attributes = get_sub_field_object('partner'); 
        $values[] = get_sub_field('partner'); 
        if($values) {
            foreach ($values as $value) 
                $all_attributes[] = $value; /* Save ALL values */
        } 
     endwhile; endif;
    
    /* use array_unique to remove duplicates from the array */
    $filter_attributes = array_unique ($all_attributes);
    ?>
    
    <?php $colors = $filter_attributes;
    	foreach ($colors as $color) {
    	    global $b;
    		$b++;
    		echo "<h3>" . ucfirst($color) . "</h3>";
    		echo '<div class="row">';
    		while ( have_rows('logos') ) : the_row();
    		    if(get_sub_field('partner') == "{$color}"):
    				// display the repeater sub field values
    				echo    '<div class="col-sm-6 col-md-3 text-center py-3" id="'.$b.'">
    						 <a href="' . get_sub_field('website') .'"><img src="' .get_sub_field('logo') . '" /></a>
    					     </div>';
    			endif;
    		endwhile;
    	echo '</div>'; 
    }?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Dynamic Repeater Fields with Titles’ is closed to new replies.