是否使用cmb2将‘SELECT’字段类型的值回显到页面模板?

时间:2015-07-08 作者:VA-YVR

我正在使用CMB2插件将元盒添加到自定义帖子类型中。我正在通过函数添加其他元数据库。php到子主题。值得一提的是,父主题/自定义帖子已经定义了一些元盒,我只是补充一下。。。

我已经成功地创建了字段类型为Text的元框,并将值回显到目标页面中,但是当字段类型为Select时,我可以创建元字段,但无法将所选选项回显到目标页面中。

你能帮帮我吗?我很感激。

Functions.php (creating the metaboxes)

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;
}

And then I output onto the page\'s content area

    <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 个回复
SO网友:Justin Sternberg

对于初学者,我建议您使用API来注册元盒和字段,可以在这里看到:https://github.com/WebDevStudios/CMB2/wiki/Basic-Usage#create-a-metabox. 回显选择选项的label, 您可以这样做:

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\' => \'Área do Terreno\',
        \'type\' => \'text\'
    ) );

    $cmb->add_field( array(
        \'id\'               => \'wiki_test_select\',
        \'name\'             => \'Test select inline\',
        \'type\'             => \'select\',
        \'show_option_none\' => true,
        \'default\'          => \'custom\',
        // Use an options callback
        \'options_cb\'       => \'wiki_test_select_options\',
    ) );

}

function wiki_test_select_options() {
    // return a standard options array
    return array(
         \'standard\' => __( \'test123\', \'ficha\' ),
         \'custom\'   => __( \'test1234\', \'ficha\' ),
         \'none\'     => __( \'test12345\', \'ficha\' ),
    );
}
然后在主题中:

<?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>

结束

相关推荐

让作者通过按下按钮向Metabox添加字段

我为作者添加了一个自定义元数据库,他们可以在其中填写指向源的url,然后我可以在模板中使用该url。在我的functions.php<?php /* Add metaboxes (see below) */ function add_custom_metaboxes(){ add_meta_box(\'source_post_metabox\', \'Link to bron (optional)\', \'output_source_metabox\', \'pos