Forked from MjHead/get-jet-engine-gallery-images.php
Created
August 10, 2022 21:01
-
-
Save Max0220/9a494377bf05170261f999beac07dd65 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 hidden or 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