Support

Account

Home Forums Add-ons Repeater Field Anchor links to scroll down to a specific ACF repeater Reply To: Anchor links to scroll down to a specific ACF repeater

  • I do this, but I do it manually.

    The first thing that I do is create a text field that will hold my unique ID.

    Then I make that field readonly

    
    add_filter('acf/prepare_field/key=field_XXXXXXX', /* field key of the text field */ function($field) {
      $field['readonly'] = true;
      return $field;
    });
    

    Then I create an acf/save_post filter that updates the field with a unique ID value

    
    add_action('acf/save_post', function($post_id) {
      // do whatever you need to get a field value
      // how you do this is dependent on how your fields
      // so there is no specific example
      // test the value of your id field to see if it is empty
      // if it is then generate a unique ID
      // sleep 1 second to ensure the value is unique
      sleep(1);
      // generate unique ID
      // https://www.php.net/manual/en/function.uniqid.php
      $id = uniqid('row-');
      // update the field with the new ID.
    });
    

    Or you could simple add a text field and allow them to enter their own ID, but then you have to worry about it they are entering something unique or not.

    In either case, then what you need to do is to alter your HTML code output to add this id to the element

    
    <div id="<?php the_field('your_field_name'); ?>">
      your content here
    </div>