Home › Forums › Front-end Issues › How can you output the Field Label? › Reply To: How can you output the Field Label?
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/
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.