Skip to content

Instantly share code, notes, and snippets.

@asufian97
Created July 3, 2018 15:23
Show Gist options
  • Save asufian97/6902ab56ddf7709c7d82527582d4f472 to your computer and use it in GitHub Desktop.
Save asufian97/6902ab56ddf7709c7d82527582d4f472 to your computer and use it in GitHub Desktop.
run loop with cmb2 metaboxes,
<!--front end html loop-->
<ul class="social">
<?php
$entries = get_post_meta( get_the_ID(), '_yourprefix_main_boxes', true );
foreach ( (array) $entries as $key => $entry ) {
$icon_name = $link_irr = '';
if ( isset( $entry['_yourprefix_font_icon'] ) ) {
$icon_name = esc_html( $entry['_yourprefix_font_icon'] );
}
if ( isset( $entry['_yourprefix_pro_link'] ) ) {
$link_irr = esc_html( $entry['_yourprefix_pro_link'] );
}
?>
<li><a href="<?php echo esc_html( $link_irr ); ?>"><i class="fab fa-<?php echo esc_html( $icon_name ); ?>"></i></a></li>
<?php
}
?>
</ul>
<!--end frontent-->
<!--backend cmb2 function.php code-->
<?php
function awc_register_repeatable_group_field_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_yourprefix_';
/**
* Repeatable Field Groups
*/
$cmb_group = new_cmb2_box( array(
'id' => $prefix . 'about_boxes',
'title' => __( 'Social link', 'cmb2' ),
'object_types' => array( 'about_us', ),
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
'id' => $prefix . 'main_boxes',
'type' => 'group',
//'description' => __( 'Generates reusable form entries', 'cmb2' ),
'options' => array(
'group_title' => __( 'Social {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Link', 'cmb2' ),
'remove_button' => __( 'Remove Link', 'cmb2' ),
'sortable' => true, // beta
),
) );
/**
* Group fields works the same, except ids only need
* to be unique to the group. Prefix is not needed.
*
* The parent field's id needs to be passed as the first argument.
*/
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Icon', 'cmb2' ),
'id' => $prefix .'font_icon',
'type' => 'text',
'desc' =>'Add font awesome5 icon, example=> fa-facebook-f'
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Link', 'cmb2' ),
'id' => $prefix . 'pro_link',
'type' => 'text_url',
'desc' =>'Add Your social link URL'
) );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment