Home › Forums › General Issues › Output Select label, based on page parent ID
Hello
I am attempting to output the label from a select list.
Here’s my code so far:
global $post;
// Get parent ID
$parent_id = $post->post_parent;
// Parent color
$color_scheme_parent = get_field('color', $parent_id);
?>
<h1>Color-scheme: color-<?php echo $color_scheme_parent; ?></h1>
This outputs the select value, but I’d like to output the select label instead.
Using the code from the docs doesn’t output anything (http://www.advancedcustomfields.com/resources/field-types/select/).
<?php
/*
* Displaying a single value's Label
*/
$field = get_field_object('color');
$value = get_field('color_scheme_name');
$label = $field['choices'][ $value ];
?>
<p>Color: <?php echo $label; ?></p>
I should say I am setting up my select list by dynamically populating it via a repeater field within Options, like so:
/**
* ACF Custom select list
* http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/
*/
function my_acf_load_field( $field )
{
// reset choices
$field['choices'] = array();
// load repeater from the options page
if(get_field('color_scheme_options', 'option'))
{
// loop through the repeater and use the sub fields "value" and "label"
while(has_sub_field('color_scheme_options', 'option'))
{
$value = get_sub_field('color_scheme_color');
$label = get_sub_field('color_scheme_name');
$field['choices'][ $value ] = $label;
}
}
// Important: return the field
return $field;
}
// v4.0.0 and above
add_filter('acf/load_field/name=color', 'my_acf_load_field');
My fields are set up like this:
1. Color: http://cld.jxpr.se/image/3o1B3u0D1X2U
2. Color options page: http://cld.jxpr.se/image/3j2H1v1C3w3n
Any pointers greatly appreciated.
Thanks
Should code block 2 be
$value = get_field('color_scheme_color');
rather than
$value = get_field('color_scheme_name');
? Just looking at your field setup in block 3, it looks like you’re setting the field choices to ‘color_scheme_color’ = ‘color_scheme_name’.
Thanks for this.
I’m still outputting nothing for the label though. With this block of code, I can happily output the $color_scheme_parent variable, which is the value set from the select list, but not the label:
<?php
/*
* Displaying a single value's Label
*/
$field = get_field_object('color');
$value = get_field('color_scheme_color');
$label = $field['choices'][ $value ];
?>
<p>Color label: <?php echo $label; ?> Parent color: <?php echo $color_scheme_parent; ?></p>
Hi @juxprose
wouldn’t the field name be ‘color_scheme_color’ instead of ‘color’?
I think your first line of code should be:
$field = get_field_object('color_scheme_color');
Thanks Elliot. Further bit of testing, from this code block:
<?php // Color scheme
// --------------------------------------------------------------------------------
// Grab the color scheme for pages. Sub pages inherit the parent's color scheme.
// http://wordpress.org/support/topic/plugin-advanced-custom-fields-field-from-parent-page-into-child-page
global $post;
// Get parent ID
$parent_id = $post->post_parent;
// Parent color
$color_scheme_parent = get_field('color', $parent_id);
?>
<?php
/*
* Displaying a single value's Label
*/
$field = get_field_object('color_scheme_color');
$value = get_field('color');
$label = $field['choices'][ $value ];
?>
Color field: <?php echo $field; ?> <br>
Color value: <?php echo $value; ?> <br>
Color label: <?php echo $label; ?> <br>
Parent color: <?php echo $color_scheme_parent; ?>
I get this output on the page (page.php):
Color field: Array
Color value: #464972
Color label:
Parent color: #464972
Do you have any other de-bugging suggestions or tests I could perform to see why I can’t output the label?
Many thanks!
This combination output the label correctly, thanks for the pointers.
<?php
/*
* Displaying a single value's Label
*/
$field = get_field_object('color');
$value = get_field('color');
$label = $field['choices'][ $value ];
?>
Another question on the same issue has raised though. I have a page, with a colorscheme set as described above, also within the page, I have a Repeater Field, which has a sub-field to set a colorscheme.
I’d like to use the same set of color options I’ve set in Options. This works fine if the sub field is named “color”, as this will pick up the choices for a select list. But when these are output, things get confused between the pages colorcheme, and the repeater field box colorscheme – both of which come from a field called “color”.
Is it possible to have a page and a repeater field using the same Options data source for a select list?
Thanks.
The topic ‘Output Select label, based on page parent ID’ 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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.