Support

Account

Home Forums Front-end Issues How can you output the Field Label?

Solving

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.

  • Hi @darrenbachan

    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?

  • Hi @darrenbachan

    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/

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

The topic ‘How can you output the Field Label?’ is closed to new replies.