WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Fields
[resolved] Getting the list of validation options and labels (7 posts)

  1. Jason Judge
    Member
    Posted 1 year ago #

    I am using a validation list to give me a drop-down list of allowed values for a custom field. The list includes values and labels, the labels being what is stored.

    Now, I would like to display the labels on a page, given the stored value. Is there an API I can use to get hold of the value/label pairs for the field, so I can do the mapping for display?

    -- Jason

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. yopraag
    Member
    Posted 1 year ago #

    Hi

    Check out this answer I gave on the support section of acf.
    http://support.advancedcustomfields.com/discussion/comment/9283#Comment_9283

    You should be able to get what you want using get_field_object.

  3. Jason Judge
    Member
    Posted 1 year ago #

    That's great - thanks. Your example uses this to get the custom field opject:

    $field_obj = get_field_object( 'field_506d7b9428874', $post_id );

    The "field_506d7b9428874", where would that come from? I'm assuming it is the custom field ID, but I cannot see that in the ACF admin screens since it is all AJAX and these codes don't appear in any URLs. Do I need to look at the page HTML source to get that? Or can I just use the "field name" slug as it appears in the admin screen?

    -- Jason

  4. yopraag
    Member
    Posted 1 year ago #

    You can view this field key in the admin area by going into Custom Fields > your field group > Screen Options (top right) > and set "Show Field Key" to Yes

    See the pic for clarification
    http://tinypic.com/r/rk7bsh/6

    This will show you the field key next to your custom fields.

    BTW,
    You can also see these keys in your MySQL db if you search for the field name in the postmeta table.

  5. Jason Judge
    Member
    Posted 1 year ago #

    Brilliant! You're a star :-) I seldom think of looking in that "screen options" drop-down panel, and forget it contains contextual display options.

    I will be using this to expand the grade codes of second-hand disks in this shop:

    http://www.soulbrother.com/shop/turn-you-to-love/

    Just a simple code is stored (M and M in that example), but I would like to display the full description of those codes, which is defined in the ACF field.

    I'll post the code I use, then close this ticket.

  6. yopraag
    Member
    Posted 1 year ago #

    Had a similar problem.

    Glad I could help.

  7. Jason Judge
    Member
    Posted 1 year ago #

    Here we go - works a dream. In the WooCommerce meta.php template, I check if the get_field_object() function exists (because it won't if ACF is not enabled), then fetch the two lists:

    if (function_exists('get_field_object')) {
                $field_obj = get_field_object( 'field_5062e50c7da52', $product->id );
                if (!empty($field_obj['choices'])) $disc_grade_choices = $field_obj['choices'];
    
                $field_obj = get_field_object( 'field_5062e62519095', $product->id );
                if (!empty($field_obj['choices'])) $sleeve_grade_choices = $field_obj['choices'];
            }

    Further down, after I fetch the custom fields into $disc_grade and $sleeve_grade, I check if those grades are an index value in the lists, then switch in the description if they are:

    if (!empty($disc_grade_choices) && !empty($disc_grade_choices[$disc_grade])) $disc_grade = $disc_grade_choices[$disc_grade];

    Always erring in the side of safety, of course, because you cannot guarantee a given plugin or theme is going to be there.

    The code isn't rocket science, I know, but may be useful to someone. Thanks again.

    -- Jason

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.