Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active November 25, 2017 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/28d6556bdc923c9b205866bbbac07b5e to your computer and use it in GitHub Desktop.
Save damiencarbery/28d6556bdc923c9b205866bbbac07b5e to your computer and use it in GitHub Desktop.
Using CMB2 instead of ACF to manage and display custom fields.
<?php
function bbc_inflatable_full_info_cmb2() {
$inflatable_info = get_post_meta( get_the_ID() );
?>
<div class="one-half first">
<?php
$images = maybe_unserialize( $inflatable_info[ 'photos_group' ][ 0 ] )[ 0 ][ 'gallery' ];
if ( count( $images ) ) { ?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$main_image = get_post_thumbnail_id();
$full_image = wp_get_attachment_image_src( $main_image, 'full' )[ 0 ];
}
else {
$full_image = reset( $images ); // Gets value of first element - the url of the image.
$main_image = key( $images ); // Get key of first element - the image ID.
unset( $images[ $main_image ] ); // Remove first photo as it will be used as main image.
}
$featured_image = wp_get_attachment_image( $main_image, 'featured-image' );
printf('<a href="%s" itemprop="image" class="main-image zoom thickbox" title="" rel="prettyPhoto[gallery] gallery">%s</a>',
$full_image, $featured_image );
if (count($images) > 1) {
?>
<div class="thumbnails">
<?php
foreach ( $images as $image_id => $full_image ) {
$image_info = wp_get_attachment_image( $image_id ); // Get thumbnail size.
printf('<a href="%s" itemprop="image" class="main-image zoom thickbox" title="" rel="prettyPhoto[gallery] gallery">%s</a>',
$full_image, $image_info );
}
?>
</div>
<?php
} // End: if (count($images) > 1) ?>
</div>
<?php
}
?>
</div><div class="one-half"><?php
ob_start();
echo '[tabby title="Description"]';
the_content();
echo '[tabby title="Dimensions"]';
$dimensions = maybe_unserialize( $inflatable_info[ 'dimensions_group' ][ 0 ] );
printf('<p>Width: %d feet', $dimensions[ 0 ][ 'width' ]);
printf('<br/>Length: %d feet', $dimensions[ 0 ][ 'length' ]);
printf('<br/>Height: %d feet', $dimensions[ 0 ][ 'height' ]);
printf('<br/>Slide Height: %d feet', $dimensions[ 0 ][ 'slide_platform_height' ]);
printf('<br/>Required Access Width: %d feet</p>', $dimensions[ 0 ][ 'required_access_width' ]);
echo '[tabby title="Ages &amp; Safety"]';
$ages_safety_info = maybe_unserialize( $inflatable_info[ 'ages_safety_group' ][ 0 ] );
echo '<p>Suitable for <strong>', $ages_safety_info[ 0 ][ 'ages' ], ' years</strong></p>';
$safety_rules = $ages_safety_info[ 0 ][ 'safety_rules' ];
if (empty($safety_rules)) {
echo 'It is the responsibility of the person or persons who hire the unit to ensure the safety of the children. Please deflate the unit if the wind becomes strong. ADULT SUPERVISION AT ALL TIMES';
}
else {
echo '<p>'.$safety_rules.'</p>';
}
echo '[tabbyending]';
echo "<h2>Venues</h2>";
echo '<p>', get_the_title(), ' will fit in: ';
echo get_the_term_list( get_the_ID(), 'venue', '', ', ' );
echo do_shortcode(ob_get_clean());
?></div>
<div class="clearfix"></div>
<?php
// The Hire contact form
echo do_shortcode('[contact-form-7 id="543" title="Hire Enquiry"]');
}
<?php
add_action( 'cmb2_admin_init', 'cmb2_belgrove_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function cmb2_belgrove_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'bbc', // Belgrove Bouncing Castles
'title' => 'Equipment Information',
'object_types' => array( 'bouncing_castles', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
// Photos
$photos_group_id = $cmb->add_field( array(
'id' => 'photos_group',
'type' => 'group',
'repeatable' => false,
'options' => array(
'group_title' => 'Photos',
),
) );
$cmb->add_group_field( $photos_group_id, array(
'name' => 'Photos',
'desc' => 'Add photos of the equipment.',
'id' => 'gallery',
'type' => 'file_list',
'preview_size' => array( 150, 150 ),
'query_args' => array( 'type' => 'image' ), // Only images attachments.
// Optional, override default text strings
'text' => array(
'add_upload_files_text' => 'Add to gallery', // default: "Add or Upload Files"
),
) );
// Dimensions
$dimensions_group_id = $cmb->add_field( array(
'id' => 'dimensions_group',
'type' => 'group',
'repeatable' => false,
'options' => array(
'group_title' => 'Dimensions',
),
) );
$cmb->add_group_field( $dimensions_group_id, array(
'name' => 'Width',
'desc' => 'Width of the inflatable in feet',
'id' => 'width',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
$cmb->add_group_field( $dimensions_group_id, array(
'name' => 'Length',
'desc' => 'Length of the inflatable in feet',
'id' => 'length',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
$cmb->add_group_field( $dimensions_group_id, array(
'name' => 'Height',
'desc' => 'Height of the inflatable in feet',
'id' => 'height',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
$cmb->add_group_field( $dimensions_group_id, array(
'name' => 'Slide Platform Height',
'desc' => 'Height of the slide platform in feet',
'id' => 'slide_platform_height',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
$cmb->add_group_field( $dimensions_group_id, array(
'name' => 'Required Access Width',
'desc' => 'Width required to move inflatable, in feet',
'id' => 'required_access_width',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
// Ages/Safety
$ages_safety_group_id = $cmb->add_field( array(
'id' => 'ages_safety_group',
'type' => 'group',
'repeatable' => false,
'options' => array(
'group_title' => 'Ages/Safety',
),
) );
$cmb->add_group_field( $ages_safety_group_id, array(
'name' => 'Ages',
'id' => 'ages',
'type' => 'text',
'attributes' => array(
'required' => 'required',
),
) );
$cmb->add_group_field( $ages_safety_group_id, array(
'name' => 'Ages',
'desc' => 'Enter age range of this inflatable eg 2-7 or 6-17. "Years" will be appended when displayed.',
'id' => 'ages',
'type' => 'text',
) );
$cmb->add_group_field( $ages_safety_group_id, array(
'name' => 'Safety Rules',
'desc' => 'List the rules regarding the use of this piece of equipment.',
'id' => 'safety_rules',
'type' => 'textarea_small',
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment