Home Forums General Issues Unique ID / Class from ACF


Unique ID / Class from ACF

  • I’m looking for a way to get a unique field name to relate a class / ID to a script.

    Here’s an example of my markup:

    <div class="UNIQUE-ACF-ID">
     $image = get_sub_field('image');
     echo $image['sizes']['large'];

    Is this done with a field key? or is there a PHP tag I can use? Not sure the best way to go about doing this.

    Ps: I can’t hard code the name because this is inside of a flexible content field and can be reused, so the classes won’t be unique then.

  • Hi @emaildano

    Is this on the front end in a template?
    Is the JS inline with the template?

    You can create a unique ID from many things such as the time, name or even field value. If both the HTML and JS are being generated in the same file, you can match the ‘selector’ quite easily.

    I prefer to use the function uniqid – which will give you a very unique number.


  • Elliot, Thanks!

    This would be front end, inline JS so I can pull the custom fields for images.

    I’m reading about the uniquid function and this looks like this will work. I’ll just need to setup a variable to use in both JS and PHP locations.

    Thanks again.

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

The topic ‘Unique ID / Class from ACF’ is closed to new replies.