Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
-
Hey Guys,
is it possible to show the field label too on the frontend? If I use the_field, there is the value only. But I need the information I typed in as field label. Anyone else wanting to do such a thing?
Regards -
Hi,
Do You mean, that the fields that will be filled in are used in frontend?
The way, You would use a form?
If this I would like that too.
What I am trying to do is:
Using fields of just every type, and those fields should be fillable from frontend users and then this data will be a post or page in frontend.
Main goal is creating a directory!!
Cheers.
Andre -
Hi,
that's not the point. I don't want to use ACF on the frontend. I just want to display the value as well as the label on the fronted. For example:
stored information backend:
label: height
value: 3m
given information on frontend with the_field :
3m
What I need on frontend:
height 3m
You know? Otherwise I have to add the label manually for every field in my loop-single.php. Looks like this at the moment:
Height
<?php the_field('product_height'); ?> m
And maybe could look something like this:
<?php the_field('product_height','label'); ?>
<?php the_field('product_height','value'); ?> m
Regards -
Hi, I would actually need the field label because I use the advanced custom fields to make taxonomy selections. For example:
On single post pages (i. e. a newspaper article) I want to chose the taxonomy which is used for a tag cloud below the article. This can be a region, a person, an organisation etc. I only want to use one because otherwise it gets to crowded.
And so I can use the advanced custom fields to make this selection. If I select "region" then this term is passed on to the tag cloud as taxonomy term.
And since I also want to put a title above the tag cloud saying "Important projects" I then need the field label, otherwise the output is just the singular term (slug) of the taxonomy. -
By the way, I could solve my problem above and show the taxonomy name (label) by using the following code:
$the_tax = get_taxonomy( get_field('taxonomy_for_tag_cloud') );
echo $the_tax->labels->name;
However, this only works becaus I use the advanced custom field to select which taxonomy is used. -
I also wanted to do this so I made a custom function to get the label for acf fields from the database...
function get_acf_labels($name = null) {
global $wpdb;
// Query DB to find either a single row or all acf field names and labels
$fields_table = $wpdb->prefix . "acf_fields";
if ($name) $fields_query = "SELECT name, label FROM $fields_table WHERE name = '$name'";
else $fields_query = "SELECT name, label FROM $fields_table";
$records = $wpdb->get_results( $fields_query );
// Sort results into associative array, using field name as key for label values
if (sizeof($records) > 0) {
foreach ($records as $record) {
$labels[$record->name] = $record->label;
}
return $labels;
} else return false;
}
That returns an array with the field labels, using the name as a key.
If a field name is given as an argument it will just return the label for that field.
Then I've used a content filter to add the list of acf field values on a post, using the label for each field...
function acf_values($content) {
// get values from ACF api, field labels from custom function
$fields = get_fields();
$labels = get_acf_labels();
// if succesful, output list of values with field labels
if ($fields && $labels) {
$content.= "<ul class='acf_values'>";
foreach($fields as $name => $value) {
$label = $labels[$name];
$content.= "<li><label>$label</label> $value</li>";
}
$content.= "</ul><!-- end acf_valus -->";
}
return $content;
}
add_filter('the_content', 'acf_values');
The reason I'm doing it this way is because I have a different set of acf fields for posts depending on their category, but I wanted a one size fits all approach for outputting a post's data that didn't require me to make templates for each category.
Hope that helps anyone having similar issues.
Great plugin btw. -
"I just can't see any situation when you would need to bring in the label when you already know the field_name."
Situation:
My client sells flats. Fields for flats at the moment are: "ID", "no_of_rooms". My clients wants to be able to add custom fields himself later, like "max_no_of_persons".
He can't change the template of course! And I cannot output the name because they are ugly.
Thanks for the plugin and the label makes A LOT of sense!. Please make it accessible, too.
@etherealtim: Thanks for your solution. -
This works doesn't seem to work anymore. Seems ACF moved its database records from "prefix_acf_fields" to "usermeta" where it now only stores the field values. I tried to play around with the API but couldn't get it to work.
-
It would be great to have a function the_label() available in the plugin. While the developer knows the label and can put it hard-coded in the template along with the_field(), we are creating a theme for clients, and they may want to modify the label name.
-
having the_label() would be great on multi-lingual sites, where not only the value of a CF has to be translated, but its name/label too!
-
Im assuming the Admin response is from Elliot, i find it frustrating that you would write off including a feature because you can't understand why other people would need it. You have a bunch of people here telling you how they need it, is that not enough? - Im working on a project right now where it would make my code messy and complicated if i hard-coded all of the label names into the theme template - Wouldn't you reconsider your position on this?
-
aaroncarr, that is exactly what I wanted to add and didn't for the fear of sounding a little harsh on a developer who has given out his plugin for free. But the fact remains.
Elliot, I've seen four or five threads in which you've downright rejected requests by replying something along, "displaying labels is useless; I don't see any real need for it; you already know the label, so why do you need it; if you want hardcode it," etc.
It's sad if you couldn't understand the need even after such requests. I've given a clearcut requirement above. Or, if you don't want to put your efforts on this please be frank; it'd be a lot better. -
It isnt pretty but i cobbled this together with some string and glue and put it in my api.php file
function get_field_label($field_name,$post_id = false)
{
global $post, $wpdb;
if(!$post_id)
{
$post_id = $post->ID;
}
$query = 'SELECT meta_value FROM wp_postmeta where meta_key = "'.get_post_meta($post_id,"_".$field_name,true).'"';
$field = unserialize($wpdb->get_var($query));
return $field['label'];
} -
Or maybe more like ...
function get_field_label($field_name,$post_id = false)
{
global $post, $acf;
if(!$post_id)
{
$post_id = $post->ID;
}
$field_key = get_post_meta($post_id, '_' . $field_name, true);
$field = $acf->get_acf_field($field_key);
return $field['label'];
}
function the_label($field_name,$post_id = false)
{
$value = get_field_label($field_name,$post_id);
$field_key = get_post_meta($post_id, '_' . $field_name, true);
$field = $acf->get_acf_field($field_key);
echo $field['label'];
} -
Hi Elliot.
here's a good reason why I'd like the Label on frontend: multi-lingual site.
I have image galleries and below each one I'd like to display some infos e.G.: the title, the author etc...
in english: field label='title', field value='the_title'
in french: field label='titre', field value='the_title_fr'
in german: field label='Titel', field value='the_title_de'
etc...
in such a case, having access to the label in front end would allow me to have half as many fields as I have right now (I have to have one field for the label and one for the value at the moment, not very practical, especially as the label value is actually doubled in the end...)
thanks aaroncarr for the workaround, I might try that, but as you say, that's ugly :p -
[I just can't see any situation when you would need to bring in the label when you already know the field_name.]
Here are a few:
1) Many times creating a whole database to store just ONE extra value is a waste of time: E.g. Storing a value of "5" for a 5-stars rating system, but calling them like "bronce, titanium, gold, platinum…"
2) Other times you CAN do a simple switch, require you to edit the php code every time a new option is offered.
3) For the ones who DO find useful this plugin (because don't know how to simply program a DB pull) and need average, or calculate in any way all the options, not only the selected one.
4) When we want to automate the display of fields, rather than statically hardcode the name in the theme, as you suggest.
I know it's hard to limit the plugin so we find the need to buy it, but until you figure out how to leave the feature out for the paid version, at least leave it in, instead of deny it ;)
Thanks. -
Hey guys,
Just wanted to clear something up.
1. I think that half of you are talking about retrieving the field label. eg: "Left Content Block".
To this, I still don't see the point. A good problem is stated by @Rumpelmuk when he says that his client will add more fields. My question to this is, if the client can't edit the template, how will this new field appear on the website?
2. The other half are talking about retrieving the label of a select / checkbox value.
Yes, this is a good idea. If there were new options added to the list, a function to get the labels would be useful. All this information of a field can be found using a function like @aaroncarr.
So guys, why not just create a function in your functions.php file that retrns all the field data as an object. Try this:<?php
function get_field_object($field_name,$post_id = false)
{
global $post, $acf;
if(!$post_id)
{
$post_id = $post->ID;
}
// allow for option == options
if( $post_id == "option" )
{
$post_id = "options";
}
// get value
$field_key = "";
if( is_numeric($post_id) )
{
$field_key = get_post_meta($post_id, '_' . $field_name, true);
}
else
{
$field_key = get_option('_' . $post_id . '_' . $field_name);
}
// default return vaue
$field = false;
if($field_key != "")
{
// we can load the field properly!
$field = $acf->get_acf_field( $field_key );
}
return $field;
}
?>
This function will return all the field data: label, name, options, etc...
Let me know if this helps and I will conciser adding this function to the core.
Thanks
Elliot -
Hi Elliot, I posted the original solution and haven't weighed in on this much because I'm not using it anymore.
To address your first point and especially "if the client can't edit the template, how will this new field appear on the website"...
I had a single template for posts in different categories, yet each category had different ACF fields. The fields were used as kind of properties for the post, which I needed to list after the content. So both the label and the value needed to be fetched programatically and only displayed if set.
I did this by adding a filter to 'the_content' which got any ACF labels/values assigned to the post and output them as a UL.
I think the ability to get field labels programatically in a template allows a lot more flexibility in such cases because you can assign or remove fields and never have to edit the template again.
Cheers
Tim. -
Hi Tim.
I was thinking that might be the case. Yes, your right. But in that case, the above function that you wrote and I tweaked would work perfectly.
So does this function help everyone out? -
does the get_field_object function above return the values for each field also, or just the field properties?
What I'm working on (as I mentioned in another thread) is a tech review site and each product has LOTS (in the hundreds) of tech specs for each product.
This would be useful for me as I'll be able to just add a loop to output Label -> Value pairs from a generic repeater field that i'd use as a container.
I tried to do it using the above function to populate an array to use in get_field but couldn't get it to work.
Thanks
-
Hi @Phoat,
Yes, this will return all the field options including the labels. The labels will be saved as text, so you will need to explode them by "\n" (I think) and then explode each row by ":" to get value => Label
Good luck! -
I don't know if you understood what I said, but I figured out a solution that works well for me (even tho I'm sure there's an easier way).
In the function you wrote above, I changed the part...if($field_key != "")
{
// we can load the field properly!
$field = $acf->get_acf_field( $field_key );
}
return $field;
To this...if($field_key != "")
{
// we can load the field properly!
$field = $acf->get_acf_field( $field_key );
$value = $acf->get_value_for_api($post_id, $field);
}
$field['value'] = $value;
return $field;
What this did was add a [value] array to the post object with all the data stored for each field.
Then I added this to my template to get label->data pairs for the repeater with notebook specs.$rows = get_field_object('notebook_specs');
echo '<ul>';
foreach( $rows['sub_fields'] as $row ):
$label = $row['label'];
$name = $row['name'];
echo '<li>' . $label . ': ' . $rows['value'][0][$name] . '</li>';
endforeach;
echo '</ul>';
I used a repeater as a container for the fields rather than have them all seperate so that I can loop through them easily to get the output I wanted.
This worked for what I was after but I'm afraid that it might not be very bullet proof. I assume (I haven't tested this) that it will break if the repeater is nested for more than two levels.
If there's a better way... I'm all ears.
This is going to save me HOURS of work. Imagine having to hardcode about 120 fields into a template for a product and to do that for 50+ different product categories.
Eliot, on a side note, I just wanted to say thanks. Your plugin has made me look like a kickass developer, when all I do is click a few buttons on the screen. For that, I am in your debt. Then again, I'm Greek. I'm in everyone's debt :P -
-
Hi Elliott!
Exactly how to use this function in a template? I tried this: get_field_object ('field_name'); but that does not thrown out anything. I think you need to tell it exactly what I need, eg. label, name, type. I would need to label. Can you help me?
Otherwise, I am very pleased with the plugin, it is very helpful to me. :)
Thank you, welcome
Gábor -
Hi mgabor
You need to pass through the actual field name. not 'field_name' -
Hi Ellitott!
I know. The "field_name" was just an example. I invite the the label of field "intezmeny_nev", but it doesn't show it. -
Hi mgabor
does this give you any results?<?php
var_dump( get_field_object('intezmeny_nev') );
?> -
Hi Elliott!
I get this text with this code:
"array(10) { ["key"]=> string(19) "field_5010fcfde1cd9" ["label"]=> string(4) "Név" ["name"]=> string(13) "intezmeny_nev" ["type"]=> string(4) "text" ["instructions"]=> string(0) "" ["required"]=> string(1) "0" ["default_value"]=> string(0) "" ["formatting"]=> string(4) "html" ["order_no"]=> string(1) "0" ["value"]=> string(20) "Gyermekvilág Óvoda" }"
Then now the label part has to be written? -
Hi mgabor
as it is an array, you can echo the value at key => "label"
so something like this will work<?php
$field = get_field_object('intezmeny_nev');
?>
<p><?php echo $field['label']; ?></p> -
Wow! Thanks! : )
It would be my last question that is it possible, that it only shows the name of the field when there is a content in it? -
Yes, please add to core. My use-case: Multi-lingual on WPML...labels need to be translated.
-
Yes, please add to the core.
-
Is there any update to this? I use the Headway Theme in combination with Loopbuddy.
I just can't get the field label to show, no matter what I try, all I can get to display is the ugly field name.
Utter misery that I find myself in. The only way I can display the field value is by using hard coded labels, that always display regardless of the custom fields that exist.
The biggest problem I have is that I'm unable to display a list of different information under the same label.
I can do it by using the WP built-in custom fields, but in Headway and with Loopbuddy, the line breaks are not respected, so if there are multiple values they all show in a row (no wpautop)
Trying to use html in the custom fields doesn't work.
ACF solves the line break problem, but it doesn't allow me to show the labels, only the names in their ugly_name_format rather than their Ugly Wart Diameters.
It is possible to use CSS to set widths on divs or spans, but it limits the length of the field labels for the purposes of alignment.
Since it is Headway and Loopbuddy, I am not able to edit the template files as such, or at least not with my current knowledge.
What I don't understand, is that when using the built-in custom post fields the Labels display fine. Why not so with ACF? Is there a reason we are being forced to use the names_with_underscores and no CAPITAL Letters or spaces?
I'm desparate :(
-
Thanks Eliot, I already know what the label is. What I am trying to say is that by using the Wordpress Framework I am currently using, I do not appear to be able to display this information on the page. All I can get is:
rug_sizes
when what I want is
Rug Sizes
I tried ACF some months ago in the hope of finding a solution to the problem, but it v3 was the same, as is the problem with every single fields plugin out there.
The only thing that works is the built-in WP custom fields, because this appears to not force me to create field names in lowercase letters and underscores, but the problem is it refuses to accept any wpautop filters, so if I want a field to accept multi-row data I have to wrap it in a div and use css margins to get everything to line up nicely.
This limits the length of the field names above and below.
What I can't find an answer to anywhere, is why this is happening.
The framework I use is called Headway, which does not use templates in the same way as ordinary themes, and the way I am formatting the display of posts is through a plugin called loopbuddy.
The problem appears to be between Loopbuddy and ACF (and all other existing fields plugins), but they refuse to answer my questions, and nobody else seems to know either. It has turned out to be one of the most frustrating things of my development career to date.
Guy
