Home Forums Front-end Issues How to display the ID of a custom field?


How to display the ID of a custom field?

  • I want to create CSS classes for a few specific fields in my theme, but the actual user-entered content for these text fields is quite long. How can I grab a field ID/value to the effect of “field_title_123001” (or something of similar nature) to use as my class name?


  • Hi @ramathorn

    You should use firebug / inspector to view the DOM.

    You will then see all the ID / class attributes that are using in the fields


  • These are not classes assigned in HTML markup that can be visually modified via CSS though, right? That is what I am wanting to achieve.

  • For example, if I have a basic text field, “field_test”, displayed via:

    <?php the_field('field_test'); ?>
    Displays the content entered via WP admin for that field.

    How can I also display, separately, a unique identifier/class from that field on the front-end?

    <?php the_field('field_test','id'); ?>
    Displays the field ID for that field regardless of content entered.

    or something to that effect?

  • Hi @ramathorn

    You are in charge of the markup in your theme, so you can write like this:

    <span class="my-custom-classname"><?php the_field('field_test'); ?></span>
  • I want this a dynamic class, though – that changes depending on the field(s) used

  • Hi @ramathorn

    I get the feeling that you are leaving something quite important out of your question.

    Are you using get_field_objects to loop through all the fields or something like that?

    Can you post the code for the loop you are using?


  • Nothing too unusual really. I don’t have the code on-hand, but take a repeater field setup, for example, where you loop through the different sub-fields…

    I would like a particular piece of my markup, contained in the loop, to have a unique class name for each instance of, say, a particular sub-field. Rather than use a $count where you just assign a number to a class (ie: class-1, class-2…) for this sub-field, I’m wondering if there is a way to pull a unique identifier for that sub-field to use as a classname in the markup (ie: fieldname-12)?

  • Hi @ramathorn

    Each sub field contains a name, key, label.

    Is this what you want? Perhaps you could use the get_sub_field_object function?

  • That sounds like it. How might I pull from the array the key?

  • Hi @ramathorn

    Please read the documentation for the get_field_object and get_sub_field_object functions.

    These will return the full field object information which you can then find the $field[‘key’] from.



  • Thanks Elliot.

    The UI of the new forum is great.

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

The topic ‘How to display the ID of a custom field?’ is closed to new replies.