1

The code below is meant to display an Advanced Custom Fields Repeater field.

I need to have unique values for the "for" attribute of the Label tag, and for the ID value of the trailing checkbox input field.

Also, in order for the field label to work correctly, I need the "for" value to match the ID value of the input field, and each value needs to be unique for each time the field is repeated or displayed on the front end.

See how this displays currently on the front end in the source: Imgur

The ID and "for" value are blank, but need to be matching values for each row of fields, but also needs to be unique per row.

<?php
if ( get_field('ingredients-list') )
{
    echo '<ul class="ingredientsList">';
    while ( has_sub_field('ingredients-list') )
    {
    echo '<li class="ingredient" itemprop="ingredients"> <label for=""> <input type="checkbox" class="check" id=""> ';
            if ( get_sub_field('quantity') ) echo '<span class="quantity">' . get_sub_field('quantity') . '</span> ';
            if ( get_sub_field('measurement') ) echo '<span class="measurement">' . get_sub_field('measurement') . '</span> ';
            if ( get_sub_field('ingredient_name') ) echo '<span class="name">'. get_sub_field('ingredient_name') .'</span>';
            if ( get_sub_field('measurement') ) echo '<span class="notes">' . get_sub_field('notes')  . ' </span>';
        echo '</label>';
    echo '</li>';
}
echo '</ul>';

} ?>

2 Answers 2

3

Try using a "counter" variable for the loop maybe?

<?php
if ( get_field('ingredients-list') )
{
    echo '<ul class="ingredientsList">';
    $count=0;
    while ( has_sub_field('ingredients-list') )
    {
        echo '<li class="ingredient" itemprop="ingredients"> <label for="check-'. $count .'"> <input type="checkbox" class="check" id="check-'. $count .'"> ';
            if ( get_sub_field('quantity') ) echo '<span class="quantity">' . get_sub_field('quantity') . '</span> ';
            if ( get_sub_field('measurement') ) echo '<span class="measurement">' . get_sub_field('measurement') . '</span> ';
            if ( get_sub_field('ingredient_name') ) echo '<span class="name">'. get_sub_field('ingredient_name') .'</span>';
            if ( get_sub_field('measurement') ) echo '<span class="notes">' . get_sub_field('notes')  . ' </span>';
        echo '</label>';
        echo '</li>';
        $count++;
    }
echo '</ul>';
}
?>
0
1

This sounds more like a php question, but you might try

<?php $uniqid = uniqid('item_');?>

to generate an unique string of the form

item_4b3403665fea6

and your html would look like:

<label for="item_4b3403665fea6"> 
<input type="checkbox" class="check" id="item_4b3403665fea6">

You can read more about it here in the PHP documentation:

http://php.net/uniqid

1
  • I did not try this method, but it looks sound. Thank you, and thanks for showing me uniqid.
    – adsf
    Apr 2, 2013 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.