Support Forum
Support › Meta Box - Beaver Themer Integrator › Shortcode will not show custom field groupResolved
Hi,
I have a strange issue that has been driving me crazy. I have a published field group that will not display on the front end via shortcode.
I have several other shortcodes that are almost identical (different custom field groups) and displaying content on the front end in Beaver Themer, but no matter what I do this one only ever returns an empty() result for the custom fields.
Can anyone give me any suggestions as to why I can retrieve and display these custom fields on the front end?
Here's the code from the custom fields settings:
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = array (
'title' => esc_html__( 'Composition', 'text-domain' ),
'id' => 'composition',
'post_types' => array(
0 => 'sock',
),
'context' => 'normal',
'priority' => 'low',
'fields' => array(
array (
'id' => $prefix . 'percent_combed_cotton',
'type' => 'number',
'name' => esc_html__( 'Combed Cotton', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_cotton',
'type' => 'number',
'name' => esc_html__( 'Cotton', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_nylon',
'type' => 'number',
'name' => esc_html__( 'Nylon', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_elastane',
'type' => 'number',
'name' => esc_html__( 'Elastane', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_spandex',
'type' => 'number',
'name' => esc_html__( 'Spandex', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_wool',
'type' => 'number',
'name' => esc_html__( 'Wool', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_merino',
'type' => 'number',
'name' => esc_html__( 'Merino Wool', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_acrylic',
'type' => 'number',
'name' => esc_html__( 'Acrylic', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
array (
'id' => $prefix . 'percent_elastic',
'type' => 'number',
'name' => esc_html__( 'Elastic', 'text-domain' ),
'max' => 100,
'step' => 1,
'columns' => 3,
),
),
);
return $meta_boxes;
}
Here is the shortcode:
/**
* @return false|string
*
* Shortcode to display compositon
*/
function sock_composition_func()
{
$sockcomp = rwmb_meta('composition');
ob_start();
if ( empty( $sockcomp ) ) {
return 'no sock composition found';
}
print_r($sockcomp); // my output code will go here but it never displays because $sockcomp always returns empty()
return ob_get_clean();
}
add_shortcode('sock_composition', 'sock_composition_func');
Hi,
There is a confusion with your shortcode, composition
is the meta box ID and the function rwmb_meta()
supports to display the field value with the field ID. Please try to create the field group
and get the value with the function rwmb_meta()
again.
For more information, please follow the documentation
https://docs.metabox.io/displaying-fields/
https://docs.metabox.io/extensions/meta-box-group/
Hi Long,
Thanks for the fast reply.
Ah... I see where I was confused now.
I assumed when I created a new Custom Fields "Field Group" using WP-Admin -> MetaBox -> Custom Fields that this was a MetaBox group that the sample code would work with. Newbie mistake.
I now see the Group option in the WP-Admin -> MetaBox -> Custom Fields UI, have moved the indivdiual fields in and I'm now getting each $key => $value back using a foreach loop.
Thank you!
So the final question I have is... how can I retrieve the field Name and Description as well as the field value?
Using the fields above (now in a group) I want to output a list of sock composition elements. For example; "Cotton 72%, Elastic 10%, Combed Cotton 18%".
It would be great if I could retrieve the field Name, Description, as well as the id and value - are you able to tell me how to do this?
I'm sure it's simple, but I'm still noobing through the documentation and haven't spotted it.
Absolutely loving MetaBox though - this is amazing.
Solved it!
Found rwmb_get_field_settings(). 😀
This is my shortcode now which is working great.
Thanks again!
/**
* @return false|string
*
* Shortcode to display compositon
*/
function sock_composition_func()
{
ob_start();
// grab the details of subfields in the composition_group MetaBox group.
$sockcomp = rwmb_meta('composition_group');
if ( empty( $sockcomp ) ) {
return 'No sock composition values have been entered.';
}
// get the subfields of the composition_group MetaBox group
$subfields = rwmb_get_field_settings('composition_group')[fields];
$composition = array();
// Loop through and add "Name: Value%" to an array.
foreach ($subfields as $subfield ) {
$composition[] = $subfield['name']. " ". rwmb_meta($subfield['id']) . "%";
}
// return as comma separated output, excluding last comma
echo '<span class="composition-list">' . implode(", ", $composition) . '</span>';
return ob_get_clean();
}
add_shortcode('sock_composition', 'sock_composition_func');