Advertisement
firoze

CMB-repeating field group-CMB

Aug 4th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1.  
  2. // call this into CMB
  3.  
  4. /**
  5. * Repeatable Field Groups
  6. */
  7. $meta_boxes['field_group_entry'] = array(
  8. 'id' => 'field_group_entry',
  9. 'title' => __( 'Repeating Field Group', 'cmb' ),
  10. 'pages' => array( 'more_etnry', ), // this is post type
  11. 'fields' => array(
  12. array(
  13. 'id' => 'repeat_group2',
  14. 'type' => 'group',
  15. 'description' => __( 'Generates reusable form entries', 'cmb' ),
  16. 'options' => array(
  17. 'group_title' => __( 'Entry {#}', 'cmb' ), // {#} gets replaced by row number
  18. 'add_button' => __( 'Add Another Entry', 'cmb' ),
  19. 'remove_button' => __( 'Remove Entry', 'cmb' ),
  20. 'sortable' => true, // beta
  21. ),
  22. // Fields array works the same, except id's only need to be unique for this group. Prefix is not needed.
  23. 'fields' => array(
  24. array(
  25. 'name' => 'Entry Title',
  26. 'id' => 'title',
  27. 'type' => 'text',
  28. // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
  29. ),
  30. array(
  31. 'name' => 'Description',
  32. 'description' => 'Write a short description for this entry',
  33. 'id' => 'description',
  34. 'type' => 'textarea_small',
  35. ),
  36. array(
  37. 'name' => 'Entry Image',
  38. 'id' => 'image',
  39. 'type' => 'file',
  40. ),
  41. array(
  42. 'name' => 'Image Caption',
  43. 'id' => 'image_caption',
  44. 'type' => 'text',
  45. ),
  46. // add more
  47. array(
  48. 'name' => 'Title Two',
  49. 'id' => 'title2',
  50. 'type' => 'text',
  51. ),
  52. ),
  53. ),
  54. ),
  55. );
  56.  
  57.  
  58.  
  59.  
  60. ****************************************************************************************************************
  61.  
  62. // usages of Repeatable Field Groups
  63.  
  64.  
  65. <div class="container">
  66. <div class="row">
  67.  
  68. <?php
  69. global $post;
  70. $args = array( 'posts_per_page' =>-1, 'post_type'=> 'more_etnry','order'=>'ASC');
  71. $myposts = get_posts( $args );
  72. foreach( $myposts as $post ) : setup_postdata($post); ?>
  73.  
  74.  
  75.  
  76. <?php
  77. $entries = get_post_meta( get_the_ID(), 'repeat_group2', true );
  78. foreach ( (array) $entries as $key => $entry ) {
  79.  
  80. $img = $title = $desc = $caption = $title2 = ''; // add more
  81.  
  82. if ( isset( $entry['title'] ) )
  83. $title = esc_html( $entry['title'] );
  84.  
  85. if ( isset( $entry['description'] ) )
  86. $desc = wpautop( $entry['description'] );
  87.  
  88. if ( isset( $entry['image_id'] ) ) {
  89. $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
  90. 'class' => 'thumb',
  91. ) );
  92. }
  93. $caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
  94.  
  95. // add more
  96. if ( isset( $entry['title2'] ) )
  97. $title2 = esc_html( $entry['title2'] );
  98.  
  99. // Do something with the data
  100. ?> <!-- back php start -->
  101.  
  102. <div class="col-md-4">
  103. <h2><?php echo $title;?></h2>
  104. <?php echo $desc;?>
  105. <?php echo $img;?>
  106. <?php echo $caption;?>
  107. <h2><?php echo $title2;?></h2>
  108. </div>
  109.  
  110. <!-- back php end --><?php
  111.  
  112. }
  113.  
  114. ?>
  115.  
  116.  
  117. <?php endforeach;?>
  118. </div>
  119. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement