Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. <?php
  2. /**
  3. * Returns an array of image IDs stored in gllery meta field
  4. *
  5. * @param string $field_key Gallery field meta key
  6. * @param int $post_id Post ID to get field from. If not set - will try to use current post.
  7. * @return array
  8. */
  9. function my_get_jet_engine_gallery( $field_key = '', $post_id = null ) {
  10.  
  11. $gallery = array();
  12.  
  13. if ( ! $post_id ) {
  14. $post_id = get_the_ID();
  15. }
  16.  
  17. if ( ! $post_id || ! $field_key ) {
  18. return $gallery;
  19. }
  20.  
  21. $meta = get_post_meta( $post_id, $field_key, true );
  22.  
  23. if ( ! $meta ) {
  24. return $gallery;
  25. }
  26.  
  27. $gallery = explode( ',', $meta );
  28.  
  29. return $gallery;
  30.  
  31. }
  32.  
  33. /**
  34. * Usage example for field with name '_gallery' and some specific post
  35. */
  36. $gallery = my_get_jet_engine_gallery( '_gallery', 156 );
  37.  
  38. foreach ( $gallery as $img_id ) {
  39. echo wp_get_attachment_image( $img_id );
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement