Home › Forums › Front-end Issues › How can you output the Field Label?
I’ve read a lot of articles where it said you need to grab the field key value. I did a var dump and saw “label”, could I not somehow target that and pull it? Right now I need to manually type it out but being able to grab the label so it’s dynamic would be way better.
You can get the label by using the get_field_object() function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get_field_object/.
I hope this helps 🙂
I’ve read this before. Using the field name doesn’t work, like if I have a dozen fields I need a dozen variables or calls using get_field_object
?
get_field_object(field_1);
get_field_object(field_2);
get_field_object(field_3);
get_field_object(field_4);
get_field_object(field_5);
This isn’t clean to me. When you do a var dump
you see [label]
why can’t that be called?
I’m afraid I don’t understand how you see the [label]
. Could you please share the code you used to see the label?
If you want to get all of the custom fields in a post/page, you can always use the get_field_objects() instead.
Thanks 🙂
I know this is an old post, but you can grab the field label from the ‘posts’ table.
if (!function_exists('getAcfLabelByName')) {
function getAcfLabelByName($fieldName = null) {
if (empty($fieldName)) {
return 'No field name specified';
}
// set a default in case there's an exception
$fieldLabel = 'Not Found';
try {
/**
* ACF stores the the Labels in the
* wp_posts table, where the ACF label is the title
* and the excerpt is the field name
*/
global $wpdb;
$tableName = "{$wpdb->prefix}posts";
$labelReturned = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $tableName
WHERE post_type = %s AND post_excerpt = %s", 'acf-field', $fieldName ));
// grab the post_title from the result
$fieldLabel = !empty($labelReturned) ? $labelReturned : 'Not Found';
} catch (Exception $e) {
$error = $e->getMessage();
error_log("There was an exception finding the ACF field label by name with the message $error");
}
return $fieldLabel;
}
}
You can then use this like:
$fieldLabel = getAcfLabelByName('field_name_here');
For what it’s worth I created a simple plugin to add this function to your site:
https://wordpress.org/plugins/get-acf-field-label-from-name/
The topic ‘How can you output the Field Label?’ 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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.