Advertisement
RUSSEL86

CMB 2 custom code

Feb 9th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. /*Display CMB2 file_list item*/
  2. ----------------------------------
  3. <?php
  4. $images = get_post_meta($post->ID, 'light_image', true);
  5. foreach( $images as $image ) {
  6. echo '
  7. <img src="' . $image. '" alt="" />';
  8. }
  9. ?>
  10. / Display CMB2 file_list item with image title its mainly needed for lightbox
  11. <?php
  12. $images_list = get_post_meta($post->ID, 'galery_list', true);
  13. foreach( $images_list as $attachment_id => $attachment_url ) {
  14. $attachment_title = get_the_title($attachment_id);
  15. echo '<a title="'.$attachment_title.'" href="' . $attachment_url. '"></a>';
  16. } ?>
  17.  
  18. / Display CMB2 file_list item with image crop size its mainly needed for lightbox
  19. <ul class="slides">
  20. <?php
  21.  
  22. $idd = get_the_ID();
  23. $images = get_post_meta($idd, 'gallery_lis', true);
  24.  
  25. if ( $images ) {
  26. foreach ( $images as $attachment_id => $img_full_url ) {
  27. // image with img tag into a tag
  28. $attachment_id;
  29. // image src only
  30. $img_full_url;
  31. // image with img tag into a tag with crop size
  32. $full = wp_get_attachment_link($attachment_id, 'crop_name');
  33. // img src with crop size
  34. $image_attributes = wp_get_attachment_image_src( $attachment_id, 'gall-image' );
  35. echo $image_attributes[0];
  36.  
  37. echo "<li>";
  38. echo $full;
  39. echo "</li>";
  40.  
  41. }
  42. }
  43. ?>
  44. </ul>
  45. resource : https://goo.gl/X6fy84
  46.  
  47. /*Display CMB2 checkbox field with condition*/
  48. ----------------------------------
  49. <?php
  50. $idd = get_the_ID();
  51. $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), '' );
  52. $video_url= get_post_meta($idd, 'video_url', true);
  53. $video_check= get_post_meta($idd, 'video_checkbox', true);
  54. if($video_check == 'on'){
  55. $img_linkk = $video_url;
  56. $imag_clss = 'mfp-iframe';
  57. } else{
  58. $img_linkk = $image[0];
  59. $imag_clss = 'gallery-items';
  60. } ?>
  61. <a class="<?php echo $imag_clss; ?>" title="<?php the_title();?>" href="<?php echo $img_linkk; ?>"></a>
  62.  
  63. ---------- or -------------
  64. <?php $pdfpop = get_post_meta($post->ID, 'pdf-pop', true); ?>
  65. <?php if($pdfpop == 'on'):?>
  66. //condition content
  67. <?php else :?>
  68. //default content
  69. <?php endif; ?>
  70.  
  71. /*Display CMB2 checkbox field with condition into query*/
  72. ----------------------------------
  73. 'meta_query' => array(
  74. array(
  75. 'key' => 'feature_checkbox',
  76. 'value' => 'on',
  77. 'compare' => 'LIKE',
  78. ),
  79. ),
  80.  
  81. /*Display CMB2 select field with condition*/
  82. ----------------------------------
  83. <?php
  84. $idd = get_the_ID();
  85. $select_big = get_post_meta($idd, 'select_big', true);
  86. if($select_big == 'Big'){
  87. $namdilam = 'grid-item--width2';
  88. } else{
  89. $namdilam = 'small_grid';
  90. }
  91. ?>
  92. <?php
  93. if($select_big == 'Big'){
  94. the_post_thumbnail('gallr-big');
  95. } else{
  96. the_post_thumbnail('galler-small');
  97. }
  98. ?>
  99.  
  100. /*Display CMB2 Date field*/
  101. ----------------------------------
  102. <?php
  103. $date = get_post_meta($post->ID, 'light_image', true);
  104. echo date("d/m/y", $date );
  105. ?>
  106.  
  107.  
  108. /*Display CMB2 Group item*/
  109. ----------------------------------
  110.  
  111. <?php
  112. $meta_entries_pre = get_post_meta( get_the_ID(), 'repeat_group', true );
  113. $meta_entries_int = array_reverse( $meta_entries_pre );
  114. $meta_entries = array_slice( $meta_entries_int, 0, 3 );
  115. if( ! empty( $meta_entries ) ) { ?>
  116.  
  117. <div class="escrts_testimonials_content fix">
  118.  
  119. <?php foreach ( (array) $meta_entries as $key => $entry ) {
  120. $product_name = $product_description = $meta_link = '';
  121. if ( isset( $entry['author_testimonial'] ) ) $product_name = $entry['author_testimonial'];
  122. if ( isset( $entry['testimonial_desc'] ) ) $product_description = $entry['testimonial_desc'];
  123. if ( isset( $entry['product_image'] ) ) $product_image = esc_html( $entry['product_image'] ); ?>
  124. <li>
  125. ?>
  126. <div class="escorts_single_testimonial fix">
  127. <div class="escorts_test_cont fix">
  128. <p>“<?php echo $product_description; ?>”</p>
  129. <img src="<?php echo $product_image; ?>" alt="" />
  130. </div>
  131. <div class="escorts_test_owner fix">
  132. <p>– <?php echo $product_name; ?></p>
  133. </div>
  134. </div>
  135. <?php } ?>
  136. </div>
  137. <?php } ?>
  138.  
  139. Limit to specific page template
  140. ==================================
  141. $cmb = new_cmb2_box( array(
  142. 'id' => 'contact-information',
  143. 'title' => 'Contact Information',
  144. 'object_types' => array( 'page' ), // post type
  145. 'show_on' => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),
  146. 'context' => 'normal', // 'normal', 'advanced', or 'side'
  147. 'priority' => 'high', // 'high', 'core', 'default' or 'low'
  148. 'show_names' => true, // Show field names on the left
  149. ) );
  150.  
  151. Allow metabox to show up everywhere except a specified list of page IDs
  152. --------------------------------------------------------------------------
  153. add_action( 'cmb2_init', 'cmb2_add_metabox' );
  154. function cmb2_add_metabox() {
  155.  
  156. $cmb = new_cmb2_box( array(
  157. 'id' => 'metabox',
  158. 'title' => __( 'Metabox Title', 'cmb2' ),
  159. 'object_types' => array( 'page', 'banner-items' ),
  160. 'context' => 'normal',
  161. 'priority' => 'default',
  162. 'exclude_ids' => array( 1, 2, 3, 55 ), // Exclude metabox on these post-ids
  163. 'show_on_cb' => 'cmb2_exclude_for_ids',
  164. ) );
  165.  
  166. $cmb->add_field( array(
  167. 'name' => __( 'this is test', 'cmb2' ),
  168. 'id' => 'baner_img',
  169. 'type' => 'file',
  170. ) );
  171.  
  172. }
  173.  
  174. function cmb2_exclude_for_ids( $cmb ) {
  175. $ids_to_exclude = $cmb->prop( 'exclude_ids', array() );
  176. $excluded = in_array( $cmb->object_id(), $ids_to_exclude, false );
  177. return ! $excluded;
  178. }
  179.  
  180.  
  181. cmb2 custom group field
  182. =========================
  183. This code for make group field as your requirement into cmb2 config.php
  184. -----------------------------------------------------------------------
  185.  
  186. $dim_group = $products_cmb->add_field( array(
  187. 'id' => $prefix.'product-dim',
  188. 'type' => 'group',
  189. 'description' => __( 'Enter Your Technical Data ', 'cmb' ),
  190. 'name' => __('Technical Data ', 'ecomed'),
  191. 'options' => array(
  192. 'group_title' => __( 'Data Group {#}', 'ecomed' ), // since version 1.1.4, {#} gets replaced by row number
  193. 'add_button' => __( 'Add Another Data', 'cmb' ),
  194. 'remove_button' => __( 'Remove Data', 'cmb' ),
  195. 'sortable' => true, // beta
  196. ),
  197. ) );
  198.  
  199. // Id's for group's fields only need to be unique for the group. Prefix is not needed.
  200. $products_cmb->add_group_field( $dim_group, array(
  201. 'name' => 'Referência',
  202. 'id' => 'ref',
  203. 'type' => 'text',
  204. ) );
  205.  
  206. // Id's for group's fields only need to be unique for the group. Prefix is not needed.
  207. $products_cmb->add_group_field( $dim_group, array(
  208. 'name' => 'Avanço',
  209. 'id' => 'avg',
  210. 'type' => 'text',
  211. ) );
  212.  
  213. // Id's for group's fields only need to be unique for the group. Prefix is not needed.
  214. $products_cmb->add_group_field( $dim_group, array(
  215. 'name' => 'Descrição',
  216. 'id' => 'desc',
  217. 'type' => 'textarea',
  218. ) );
  219.  
  220. // Id's for group's fields only need to be unique for the group. Prefix is not needed.
  221. $products_cmb->add_group_field( $dim_group, array(
  222. 'name' => 'Unidade',
  223. 'id' => 'unit',
  224. 'type' => 'text',
  225. ) );
  226.  
  227.  
  228. this code for display them
  229. ---------------------------------------
  230. <?php
  231. $product_dim = get_post_meta( get_the_ID(), $prefix.'product-dim', true );
  232. foreach ( (array) $product_dim as $key => $entry ) {
  233.  
  234. $ref = $avg = $desc = $unit = '';
  235.  
  236. if ( isset( $entry['ref'] ) )
  237. $ref = esc_html( $entry['ref'] );
  238. if ( isset( $entry['avg'] ) )
  239. $avg = esc_html( $entry['avg'] );
  240. if ( isset( $entry['desc'] ) )
  241. $desc = esc_html( $entry['desc'] );
  242. if ( isset( $entry['unit'] ) )
  243. $unit = esc_html( $entry['unit'] );
  244.  
  245. ?>
  246.  
  247. <div class="sproduct_table_col">
  248. <div class="sproduct_row_ref"><p><?php echo $ref; ?></p></div>
  249. <div class="sproduct_row_avg"><p><?php echo $avg; ?></p></div>
  250. <div class="sproduct_row_des"><p><?php echo $desc; ?></p></div>
  251. <div class="sproduct_row_unit"><p><?php echo $unit; ?></p></div>
  252. </div>
  253. <?php }
  254. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement