Support

Account

Home Forums Add-ons Repeater Field count repeater fields, create columns

Helping

count repeater fields, create columns

  • I want to display a different amount of columns depending on the amount of sub fields in a repeater field. for example
    if there are 2 fields, class d-1of2
    if there are 3 fields, class d-1of3
    if there are 4 fields, class d-1of4
    and so on.

    here’s my example for 2 fields:

    <?php if( have_rows('spalten') ): ?>
        <?php while( have_rows('spalten') ): the_row(); 
            // vars
            $title = get_sub_field('spaltentitel');
            $content = get_sub_field('spalteninhalt');
            ?>
            <div class="d-1of2">
                <h2><?php echo $title; ?></h2>
                <?php echo $content; ?>
            </div>
        <?php endwhile; ?>
    <?php endif; ?>

    is this possible?

  • Hi @irenehofer,

    Yup, it’s possible; there’s a few different strategies.
    One you could use something alone the lines of:

    $count = count( get_field( 'spalten' ) );
    echo $count;

    To then concatenate + create the class name you’d like and then echo it where desired.

    A more elaborate way is detailed in this thread, if you want to check that out as well. Let me know if there’s anything I can clarify!

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

You must be logged in to reply to this topic.