• Resolved vrhovc

    (@vrhovc)


    Probably it is a basic thing, but I don’t know hot to display a select fields value on a custom template page.

    I only get the key

    $cmb->add_field( array(
            'name'             => __( 'Item Unit', 'cmb2' ),
            'desc'             => __( 'field description (optional)', 'cmb2' ),
            'id'               => $prefix . 'item_unit',
            'type'             => 'select',
            'show_option_none' => false,
            'options'          => array(
                'kg'           => __( 'Kilogram', 'cmb2' ),
                'g'            => __( 'gram', 'cmb2' ),
                'l'            => __( 'liter', 'cmb2' ),
            ),
        ) );

    when using the
    echo get_post_meta( get_the_ID(), $prefix.’item_unit’, true );

    I can only get “kg”, but I want to get “Kilogram”

    Is there any page where there is a list of all the fields and their display method(examples with different options)?

    Thanks.

    https://wordpress.org/plugins/cmb2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I have to wager that with the options array you’re providing, the “kg” key is the value actually being saved to the the meta key, and the “Kilogram” on the right is simply used for display.

    Are you for sure needing the abbreviated versions to be saved? or could you change those to the full wording for the measurements?

    Thread Starter vrhovc

    (@vrhovc)

    Thanks for the answer.

    So I guess I don’t “need” those values and I just have to enter the whole string no matter how long it is.
    (the kg=>Kilogram was just one example that I need)

    I presume that with other similar fields it is the same.

    What about in the case I need some multi language?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    How to handle the internationalization is a valid question.

    The more I think about this case, the less “one definite solution” it becomes. Part of me wants to say when doing the display, do an if statement to check the resulting meta value and output the long name based on that. Another option would be a helper function.

    function vrhovc_get_measurement( $post_id = 0 ) {
        $measurement = get_post_meta( $post_id, 'meta_key', true );
        if ( $measurement == 'g' ) {
            return __( 'gram', 'my-text-domain' );
        } else if ( $measurement == 'kg' ) {
            return __( 'kilogram', 'my-textdomain' );
        }
        ...
    }

    You’d need to replace the meta key with the actual key used, as well as the textdomain, but it’d provide an easy, 1 update location for the topic, and cover the internationalization based on value saved.

    Edit: You’d also need to finish filling in all the measurements needed.

    Thread Starter vrhovc

    (@vrhovc)

    Thanks.

    What about the other way?
    So far I understood that select field can have a function to display/define options, so would that be a better way?
    Yes the options are fixed, not dynamic, but maybe this way the sollution is more elegant.

    and You would have the options all in one place.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The original way just had this instead, in case it wasn’t clear:

    'kilogram' => __( 'Kilogram', 'cmb2' ),

    The first part is what gets saved to the meta value, which is why I suggest changing that. However, it’s not greatly translatable that way.

    The “best” way here is really just which way you’d prefer to deal with it with your site.

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

The topic ‘How to display fields in template’ is closed to new replies.