Home › Forums › Front-end Issues › 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?
Thanks
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
Thanks
E
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?
Thanks
E
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?
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.
Simple
Cheers
E
The topic ‘How to display the ID of a custom field?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.