Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Advanced Custom Fields, Echo select field label as well as value WordPress

  • SOLVED

I am using the wordpress [[LINK href="http://www.advancedcustomfields.com/docs/field-types/select/"]]Advanced Custom Fields plugin[[/LINK]].

This question is about the [[LINK href="http://www.advancedcustomfields.com/docs/field-types/select/"]]select field[[/LINK]], and in the documentation only tells me how to output the value, not the label/name.

I can't see this functionally in the documentation and was wondering if anyone had any ideas?

For example, to create Select field content, you simple do this...

gb : English
fr : Français
it : Italiano
de : Deutsch
pt : Português
es : Español
nl : Nederlands
be : Belgian
dk : Dansk
fi : Suomi
no : Norske
cz : Český
pl : Polski
hu : Magyar
ch : Schweiz
at : Österreich
eu : International



Then to output this, you write this...

<?php the_field('language'); ?>


For example if I select Polski in my post editor, the PHP will echo this value...

pl


I'm wondering if its possible to echo the label, I also want to be able to echo Polski, as well as the value pl.

Working answer gets prize amount. Thanks



-----------------------------------------------------------



Documentation updated by Elliot at ACF

I've just added the neccessary code to do this to the select documentation:
http://www.advancedcustomfields.com/docs/field-types/select/

Cheers
Elliot

Answers (2)

2013-01-07

Dbranes answers:

Hi, have you tried:
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'];
}


from

http://support.advancedcustomfields.com/discussion/290/field-label-on-frontend/p1


Josh Cranwell comments:

I tried reading this post earlier but I could not understand how to get it to work in my case.

So if I put this in my functions, how do I echo the select label for my field?


Thanks


Josh Cranwell comments:

Currently I am using this...

<?php echo get_sub_field('translation_language'); ?>


It is a sub field, not sure if this will cause an issue.


Dbranes comments:

ok, I checked the ACF database structure, and made this function that seems to work:

function get_field_label_mod($group_name,$field_name){
global $post, $wpdb;

$query_pid = 'SELECT meta_value FROM '.$wpdb->posts.' where post_title = %s';
$pid= $wpdb->get_var($wpdb->prepare($query_id,$group_name));

if($pid>0){
$query_field='SELECT meta_value FROM '.$wpdb->postmeta.' where meta_key = %s';
$field = $wpdb->get_var($wpdb->prepare($query_field,"_".$field_name));

if(strlen($field)>0){
$query_label = 'SELECT meta_value FROM '.$wpdb->postmeta.' where meta_key = %s';

$label = $wpdb->get_var($wpdb->prepare($query_label,$field));
$label = unserialize( $label);
}
}
return $label;
}


To use it

<?php

$labels=get_field_label_mod('group_name','field_name');

print_r($labels);

//get the label for 'gb'
echo $labels['choices']['gb'];
// this returns "English"
?>


where you have to modify the two values <em>'group_name'</em> and <em>'field_name'</em>.


Dbranes comments:

another usage example for the above function:

<?php
// settings to EDIT
$field_group="lang";
$field_name="translation_language";

// get all the labels
$labels=get_field_label_mod($field_group,$field_name);

// the current translation language key:
$l=get_field($field_name);

// the current translation language label:
echo $labels['choices'][$l];
?>


this works on my install.


Dbranes comments:

ok Elliot's code works also for me ;-)

you should test:


<?php
$field = get_field_object('translation_language');
print_r($field);
$value = get_field('lang');
$label = $field['choices'][ $value ];
echo "label:".$label;
?>


(get_field_object is returning similar array as get_field_label_mod above)



Josh Cranwell comments:

OK I will do,

Thanks for you help dbraines!


Josh

2013-01-07

Arnav Joy answers:

define following in functions.php

function get_selectbox_label($acf_field_name,$post_id = false,$selected_field)

{

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,"_".$acf_field_name,true).'"';

$field_data = unserialize($wpdb->get_var($query));


return $field_data['choices'][$selected_field];

}



and use it as

$your_acf_field_name = 'language';
$field_label = get_selectbox_label($your_acf_field_name,get_the_ID(),get_field($your_acf_field_name) );