SHARE
TWEET

Untitled

a guest Aug 31st, 2016 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. add_filter( 'rwmb_meta_boxes', 'prefix_attached_images_meta_box' );
  3. function prefix_attached_images_meta_box( $meta_boxes ) {
  4.     $meta_boxes[] = array(
  5.         'title'  => esc_html__( 'Attached images', 'textdomain' ),
  6.         'fields' => array(
  7.             array(
  8.                 'type'     => 'custom_html',
  9.                 'callback' => 'prefix_get_images',
  10.             ),
  11.         ),
  12.     );
  13.     return $meta_boxes;
  14. }
  15.  
  16. function prefix_get_images() {
  17.     $post_id = false;
  18.     if ( isset( $_GET['post'] ) ) {
  19.         $post_id = intval( $_GET['post'] );
  20.     } elseif ( isset( $_POST['post_ID'] ) ) {
  21.         $post_id = intval( $_POST['post_ID'] );
  22.     }
  23.     if ( ! $post_id ) {
  24.         return '';
  25.     }
  26.  
  27.     $attachments = get_children( array(
  28.         'post_parent'    => $post_id,
  29.         'post_type'      => 'attachment',
  30.         'post_mime_type' => 'image',
  31.         'numberposts'    => - 1,
  32.         'post_status'    => 'any',
  33.     ) );
  34.     if ( empty( $attachments ) ) {
  35.         return esc_html__( 'No images attached', 'textdomain' );
  36.     }
  37.     $output = '';
  38.     foreach ( $attachments as $attachment ) {
  39.         $output .= wp_get_attachment_image( $attachment->ID, 'thumbnail' );
  40.     }
  41.     return $output;
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top