Support

Account

Home Forums Add-ons Repeater Field If repeater field has 1 rows, 2 rows etc

Solved

If repeater field has 1 rows, 2 rows etc

  • I was wondering if this is at all possible:

    I’d like a repeater loop that basically has some sort of count that says if row 1 has value then do this, if row 2 has a value do this and this.

    Basically what I’m trying to achieve is if row 1 has a value display a full width column, but if there are 2 rows then display row 1 in a div then row 2 in another div etc etc.

    So basically something like

    if there is only 1 row present then:
    <div class="full"> SUBFIELD 1</div>

    If there are 2 row’s present then:

    <div class="half"> Subfield 1</div>
    <div class="half">Subfield 2</div>

    If there are 3 row’s present then:

    <div class="third"> Subfield 1</div>
    <div class="third">Subfield 2</div>
    <div class="third">Subfield 3</div>

    I could do it simply with just some if has value, but all 3 fields would be present. Was wondering if its possible with the repeater?

    Many thanks!

  • In your frontend, you’ll have to count number of elements in the repeater field array. So something like this:

    <?php
    
    // pre define the allowed columns
    $allowed_classnames = array(
        1 => 'full',
        2 => 'half',
        3 => 'third',
        4 => 'fourth',
        5 => 'fifth',
    );
    // get the count on the repeater field
    // mabye use get_sub_field() instead of get_field() if it's nested
    $number_of_cols = count( get_field( '<repeater>' ) );
    
    // set a default classname
    $classname_to_use = $allowed_classnames[1];
    
    // check if the $number_of_cols exist in the predefined classnames
    if ( array_key_exists( $number_of_cols , $allowed_classnames ) ) {
        // set the classname to be used
        $classname_to_use = $allowed_classnames[$number_of_cols];
    }
    
    while( has_sub_field( '<repeater>' ) ) : ?>
        <div class="<?php echo esc_attr( $classname_to_use ); ?>">
            <?php the_sub_field( '<repeater_content>' ); ?>
        </div>
    <?php endwhile; ?>
    

    Please not that I just briefly tested it on a site I’m working on. You’ll have to replace the '<repeater_content>' and '<repeater>' with your actual field names.

  • You sir are a genius!

    I knew i had to have a count in there somewhere just wasnt sure how to implement it – this is exactly what I was after 🙂

  • @neverything, thanks for your post. I used your example to make something for a project I’m working on, but all I needed to do was count number of field and apply different conditions. Here is my stripped down example below:

    <?php
    // get the count on the repeater field
    // mabye use get_sub_field() instead of get_field() if it's nested
    $count = count( get_sub_field( 'the_field' ) );
    
    // begin $count conditions
    if ( $count > 1 ) { ?>
      // greater than 1
      <?php the_field( 'great_than_1' ); ?>
    <?php } else { ?>
      // less than 1
      <?php the_field( 'less_than_1' ); ?>
    <?php } ?>

    https://gist.github.com/emaildano/6b6c47ec2bc3be54fcc6

  • Cool @emaildano, thanks for sharing your solution.

  • Awesome! Just what I was looking for. Thanks all above 🙂

  • How to get Image Url
    In setting i check image url and name of field is ‘image’

    in editor i write this
    the_field('image', "user_27" );

    but it returns me image id not url

    how can i fix this???

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘If repeater field has 1 rows, 2 rows etc’ is closed to new replies.