admin管理员组文章数量:1321059
I'm using CMB2 plugin to add metaboxes into a custom post type. I'm adding the additional metaboxes via functions.php onto child theme. It may worth to mention that parent theme/custom post already has a few metaboxes defined, I'm just complementing...
I already successfully created metaboxes with field type = Text and echo values into target page, however when filed type = Select I can create the meta field but can't echo the selected option into target page.
Could you please help me out? I appreciate it.
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>
I'm using CMB2 plugin to add metaboxes into a custom post type. I'm adding the additional metaboxes via functions.php onto child theme. It may worth to mention that parent theme/custom post already has a few metaboxes defined, I'm just complementing...
I already successfully created metaboxes with field type = Text and echo values into target page, however when filed type = Select I can create the meta field but can't echo the selected option into target page.
Could you please help me out? I appreciate it.
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>
Share
Improve this question
edited Sep 25, 2015 at 16:54
Travis Northcutt
3,4005 gold badges43 silver badges60 bronze badges
asked Jul 8, 2015 at 20:10
VA-YVRVA-YVR
411 silver badge3 bronze badges
1
|
1 Answer
Reset to default 4For starters, I recommend you use the API for register metaboxes and fields which can be seen here: https://github/WebDevStudios/CMB2/wiki/Basic-Usage#create-a-metabox. To echo the select option's label, you would do something like this:
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' ),
);
}
And then in the theme:
<?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>
本文标签: functionsecho value from 39select39 field type into page template using cmb2
版权声明:本文标题:functions - echo value from 'select' field type into page template using cmb2? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742090666a2420247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$test = get_post_meta( $metafield_id, REALIA_PROPERTY_PREFIX . 'wiki_test_select', true );
since your select box is namedwiki_test_select
notoptions
– czerspalace Commented Jul 9, 2015 at 23:31