First download the cmb2 files and paste the folder (e.g. cmb2) theme in the root folder of our theme. Create a functions.php file inside that folder. We will write all metabox codes in this file.
include the code in main functions.php
require_once('cmb/init.php');
require_once('cmb/functions.php');
write the following code inside cmb2/functions.php file
function amra_metabox_banabo(array $ourmetabox){
$ourmetabox[] = array(
'id' => 'first-section',
'title' => 'what is this post is all about',
'object_types' => array('page'),
'fields' => array(
array(
'name' => 'type your opinion',
'type' => 'text',
'id' => 'our-opinion'
)
)
);
return $ourmetabox;
}
add_filter('cmb2_meta_boxes', 'my_meta');
and the write the following code inside while loop of the place where we want to show the value of meta box
<?php echo get_post_meta(get_the_ID(), 'our-opinion', true); ?>
CMB2 for specific pages:
function komola_cmb(array $ourmetabox){
$ourmetabox[] = array(
'id' => 'first-section',
'title' => 'My cmb2 meta box info',
'object_types' => array('page'),
'show_on' => array(
'key' => 'page-template',
'value' => 'service.php'
),
'fields' => array(
array(
'name' => 'type your opinion',
'type' => 'text',
'id' => 'our-opinion'
)
)
);
return $ourmetabox;
}
add_filter('cmb2_meta_boxes', 'komola_cmb');
Leave a Reply