Come visualizzare il valore di un campo 'select' nel template di pagina usando CMB2?

8 lug 2015, 23:10:15
Visualizzazioni: 5.32K
Voti: 4

Sto utilizzando il plugin CMB2 per aggiungere metabox a un custom post type. Aggiungo i metabox aggiuntivi tramite functions.php nel child theme. Vale la pena menzionare che il tema genitore/custom post ha già alcuni metabox definiti, sto solo integrando...

Ho già creato con successo metabox con field type = Text e visualizzato i valori nella pagina di destinazione, tuttavia quando il field type = Select riesco a creare il meta field ma non riesco a visualizzare l'opzione selezionata nella pagina target.

Potreste aiutarmi? Lo apprezzerei molto.

Functions.php (creazione dei metabox)

add_action( 'cmb2_meta_boxes', 'custom_metabox' );

function custom_metabox( array $metaboxes ) {
    $metaboxes[REALIA_PROPERTY_PREFIX . 'ficha_tecnica'] = array(
        'id'                        => REALIA_PROPERTY_PREFIX . 'ficha',
        'title'                     => 'Ficha Técnica',
        'object_types'              => array( 'property' ),
        'context'                   => 'normal',
        'priority'                  => 'high',
        'show_names'                => true,
        'fields'                    => array(

            array(
                'id'    => REALIA_PROPERTY_PREFIX . 'area_terreno',
                'name'  => 'Área do Terreno',
                'type'  => 'text'
            ),

            array(
                'id'               => 'wiki_test_select',
                'name'             => 'Test select inline',
                'type'             => 'select',
                'show_option_none' => true,
                'default'          => 'custom',
                'options'          => array(
                    'standard' => __( 'test123', 'ficha' ),
                    'custom'   => __( 'test1234', 'ficha' ),
                     'none'     => __( 'test12345', 'ficha' ),
            ),
        ),
    ),
);

return $metaboxes;

}

E poi visualizzo nell'area contenuto della pagina

    <div class="col-sm-12 <?php if ( ! empty( $images ) ) : ?>col-md-5<?php else : ?>col-md-12<?php endif; ?>">
    <div class="property-list">
        <h2><?php echo __( 'Property overview', 'realia' ); ?></h2>

        <dl>
            <?php $price = Realia_Price::get_property_price(); ?>
            <?php if ( ! empty( $price ) ) : ?>
                <dt><?php echo __( 'Price', 'realia' )?></dt><dd><?php echo wp_kses( $price, wp_kses_allowed_html( 'post' ) ); ?></dd>
            <?php endif; ?>

            <?php $area = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'attributes_area', true ); ?>
            <?php if ( ! empty( $area ) ) : ?>
                <dt><?php echo __( 'Área do Terreno', 'realia' ); ?></dt><dd><?php echo esc_attr( $area ); ?> <?php echo get_theme_mod( 'realia_measurement_area_unit', 'sqft' ); ?></dd>
            <?php endif; ?>

            <?php $metafield_id = get_the_ID(); ?>
            <?php $test = get_post_meta( $metafield_id, REALIA_PROPERTY_PREFIX . 'options', true ); ?>
            <?php if ( ! empty( $test ) ) : ?>
                <dt><?php echo __( 'test123', 'realia' ); ?></dt><dd><?php echo $test; ?></dd>
            <?php endif; ?>

        </dl>
    </div><!-- /.property-list -->
</div>
1
Commenti

Puoi provare $test = get_post_meta( $metafield_id, REALIA_PROPERTY_PREFIX . 'wiki_test_select', true ); dato che la tua select box si chiama wiki_test_select e non options

czerspalace czerspalace
10 lug 2015 02:31:20
Tutte le risposte alla domanda 1
0

Per iniziare, ti consiglio di utilizzare l'API per registrare metabox e campi che puoi vedere qui: https://github.com/WebDevStudios/CMB2/wiki/Basic-Usage#create-a-metabox. Per visualizzare l'etichetta dell'opzione selezionata, dovresti fare qualcosa del genere:

add_action( 'cmb2_admin_init', 'custom_metabox' );

function custom_metabox() {
    $cmb = new_cmb2_box( array(
        'id'           => REALIA_PROPERTY_PREFIX . 'ficha_tecnica',
        'title'        => 'Ficha Técnica',
        'object_types' => array( 'property' ),
    ) );

    $cmb->add_field( array(
        'id'   => REALIA_PROPERTY_PREFIX . 'area_terreno',
        'name' => 'Area del Terreno',
        'type' => 'text'
    ) );

    $cmb->add_field( array(
        'id'               => 'wiki_test_select',
        'name'             => 'Test select inline',
        'type'             => 'select',
        'show_option_none' => true,
        'default'          => 'custom',
        // Usa una callback per le opzioni
        'options_cb'       => 'wiki_test_select_options',
    ) );

}

function wiki_test_select_options() {
    // restituisce un array standard di opzioni
    return array(
         'standard' => __( 'test123', 'ficha' ),
         'custom'   => __( 'test1234', 'ficha' ),
         'none'     => __( 'test12345', 'ficha' ),
    );
}

E poi nel tema:

<?php
$metafield_id = get_the_ID();
$options = wiki_test_select_options();
$key = get_post_meta( $metafield_id, 'wiki_test_select', true );
$option_name = isset( $options[ $key ] ) ? $options[ $key ] : $options['custom'];
?>
<dt><?php echo $option_name; ?></dt><dd><?php echo $key; ?></dd>
15 mar 2016 03:08:37