Created
September 26, 2019 08:37
-
-
Save MjHead/49a85b10fd04090e1cffe87280f03370 to your computer and use it in GitHub Desktop.
Example how to get an array of images stored in JetEngine gallery field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns an array of image IDs stored in gllery meta field | |
* | |
* @param string $field_key Gallery field meta key | |
* @param int $post_id Post ID to get field from. If not set - will try to use current post. | |
* @return array | |
*/ | |
function my_get_jet_engine_gallery( $field_key = '', $post_id = null ) { | |
$gallery = array(); | |
if ( ! $post_id ) { | |
$post_id = get_the_ID(); | |
} | |
if ( ! $post_id || ! $field_key ) { | |
return $gallery; | |
} | |
$meta = get_post_meta( $post_id, $field_key, true ); | |
if ( ! $meta ) { | |
return $gallery; | |
} | |
$gallery = explode( ',', $meta ); | |
return $gallery; | |
} | |
/** | |
* Usage example for field with name '_gallery' and some specific post | |
*/ | |
$gallery = my_get_jet_engine_gallery( '_gallery', 156 ); | |
foreach ( $gallery as $img_id ) { | |
echo wp_get_attachment_image( $img_id ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment