Title: ninjaskor's Replies | WordPress.org

---

# ninjaskor

  [  ](https://wordpress.org/support/users/ninjaskor/)

 *   [Profile](https://wordpress.org/support/users/ninjaskor/)
 *   [Topics Started](https://wordpress.org/support/users/ninjaskor/topics/)
 *   [Replies Created](https://wordpress.org/support/users/ninjaskor/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/ninjaskor/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/ninjaskor/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/ninjaskor/engagements/)
 *   [Favorites](https://wordpress.org/support/users/ninjaskor/favorites/)

 Search replies:

## Forum Replies Created

Viewing 10 replies - 1 through 10 (of 10 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to see if array is empty before executing foreach?](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/#post-6104240)
 * No biggey,
    **The Q: can I have the same variables within two different foreach-
   loops?** A: Yes
 * **Q: Is the if ( $id == true ) {} in the foreach a valid approach? **
    A: this
   says, if variable $id is true, do something – so I think this is valid 🙂 But
   a better way may be **if( !empty($id) )** that says, if the variable is not (
   the !) empty, do something.
 * Let me konw ^^
 * Peace & Love
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to see if array is empty before executing foreach?](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/#post-6104021)
 * No, if I use the break; on any of the two loops I get only one image.
 * so question now:
 * **Q: Is the if ( $id == true ) {} in the foreach a valid approach? And can I 
   have the same variables within two different foreach-loops? ** (no, I do not 
   want to pass the variable value, just let them have the same name for simplicity.
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to see if array is empty before executing foreach?](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/#post-6104020)
 * hmm… the break; seemes to make the foreach only display one image with a limited
   number (get_post_gallery_ids($post->ID,4);), likeso:
 *     ```
       <?php 
   
       $galleryarray = get_post_gallery_ids($post->ID,4);
   
       foreach ($galleryarray as $id) { 
   
       if ( $id == true ) {
   
       $imagesize = wp_get_attachment_image_src( $id, 'xsmall' ); 
   
       $attachment_meta = wp_get_attachment( $id );
   
       ?>
   
       <figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);">
   
       </figure>
   
       <?php break; }
   
       } ?>
       ```
   
 * So I removed the break; from this loop…
 * But without a limitation number: get_post_gallery_ids($post->ID);
    the break;
   works!
 * **Is it important to have the break; or can I just exclude it from both foreach
   loops? **
 * Any takers?
 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[Featured Galleries] Great plugin for Theme Developers](https://wordpress.org/support/topic/great-plugin-for-theme-developers-1/)
 *  [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/great-plugin-for-theme-developers-1/#post-7864033)
 * Skip the UI!! And make this 5 stars fo stats 😉
 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[Featured Galleries] Simple and lifesaving](https://wordpress.org/support/topic/simple-and-lifesaving/)
 *  [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/simple-and-lifesaving/#post-7960636)
 * Sorz, did miss the word backend – the previous link is for frontend!
 * So if your are looking for backend, check this support thread:
    [https://wordpress.org/support/topic/in-admin-metabox-with-thumbnail-or-medium-image?replies=3](https://wordpress.org/support/topic/in-admin-metabox-with-thumbnail-or-medium-image?replies=3)
 *   Forum: [Reviews](https://wordpress.org/support/forum/reviews/)
    In reply to:
   [[Featured Galleries] Simple and lifesaving](https://wordpress.org/support/topic/simple-and-lifesaving/)
 *  [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/simple-and-lifesaving/#post-7960635)
 * Saw your feedback, I solved 1_ yesterday to get custom size images.
 * See: [https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty?replies=4](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty?replies=4)
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to see if array is empty before executing foreach?](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-see-if-array-is-empty-before-executing-foreach/#post-6104003)
 * So, now i’m only looking for an extra eye on the php, is it valid? It works, 
   np, but all feedback is welcome!
 * For an example of what is possible (and what I did), see the code, **and in relevans
   with my question – is my ‘if’ in the foreach followed by ‘break;’ a good way 
   to go? **
 *     ```
       <?php 
   
       $galleryarray = get_post_gallery_ids($post->ID);
   
       foreach ($galleryarray as $id) { 
   
       if ( $id == true ) {
   
       $imagesize = wp_get_attachment_image_src( $id, 'medium' );
   
       $attachment_meta = wp_get_attachment( $id );
   
       $media_caption = $attachment_meta['caption'];
   
       $media_description = $attachment_meta['description'];
   
       ?>
   
       	<figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);">
   
       		<?php if ( $media_caption == true || $media_description == true ) { ?>
   
       			<input id="info<?php echo $id; ?>" class="nav-check" type="checkbox">
   
       			<label for="info<?php echo $id; ?>" class="nav-button info" data-title="Show Info"></label>
   
       			<figcaption>
   
       			<?php
   
       			if ( $media_caption == true ) { ?>
   
       				<h3><?php echo $media_caption; ?></h3>
   
       			<?php } 
   
       			if ( $media_description == true ) { ?>
   
       				<p><?php echo $media_description; ?></p>
   
       			<?php } ?>
   
       			</figcaption>
   
       		<?php } ?>
   
       	</figure>
   
       <?php break;
   
       }
   
       } ?>
       ```
   
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to retrieve custom image size and caption in the foreach, & break if empty?](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/#post-6095837)
 * Hopefully you can see the potentials with this plugin above. And I hate limitations,
   so this plugin is perfect.
 * **I will mark this question as solved and start a new thread to see if someone
   can give a hint about how to see if the array is empty. **
 * Happy coding!
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to retrieve custom image size and caption in the foreach, & break if empty?](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/#post-6095672)
 * Two down, one to go! 😀
 * **So, you want to get image meta like title, caption, description or whatnot?**
 * Then you need to add following in functions php (code is regardles of anything,
   use it to whatever image from the db – everywhere):
 *     ```
       function wp_get_attachment( $attachment_id ) {
   
       	$attachment = get_post( $attachment_id );
   
       	return array(
       	    'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
       	    'caption' => $attachment->post_excerpt,
       	    'description' => $attachment->post_content,
       	    'href' => get_permalink( $attachment->ID ),
       	    'src' => $attachment->guid,
       	    'title' => $attachment->post_title
       	);
   
       }
       ```
   
 * **But hey! How to display it?**
 * So, once again I edited the code and added a variable with our $id, then select
   the info I want to retrieve, like:
 *     ```
       <?php 
   
       $galleryarray = get_post_gallery_ids($post->ID);
   
       foreach ($galleryarray as $id) {
       	// select your image size
       	$imagesize = wp_get_attachment_image_src( $id, 'xsmall' );
       	// get all the meta with our $id
       	$attachment_meta = wp_get_attachment( $id );
   
       	?>
   
       	<figure style="background-image: url(<?php echo $imagesize[0]; ?>);">
   
       		<a data-name="<?php echo $id; ?>" id="<?php echo $id; ?>" href="<?php the_permalink(); echo '#' . $id; ?>">
   
       		</a>
   
       		<figcaption><h3>
   
       		<?php
       		// I wish to display the image Title, so...
       		echo $attachment_meta['title']; ?>
   
       		</h3></figcaption>
   
       	</figure>
   
       <?php } ?>
       ```
   
 * PAO! Ninjaslap.
 * **Question to creator of plugin,**
    Is there a better way? Or simply integrate
   the function for the plugin? I mean, the meta for every image is supernice to
   have handy 😀
 * **And do someone know how to not display -anything- if the called array is empty
   with the foreach?**
 * All best, /ninjaskor
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Featured Galleries] How to retrieve custom image size and caption in the foreach, & break if empty?](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/)
 *  Thread Starter [ninjaskor](https://wordpress.org/support/users/ninjaskor/)
 * (@ninjaskor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/how-to-retrieve-custom-image-size-and-caption-in-the-foreach-break-if-empty/#post-6095648)
 * One down, two to go!
 * I added a variable that fetchs $id in foreach and gets the image size, superb
   for loadtime on different media!
 * For custom image size (declared in functions.php, [see here](https://codex.wordpress.org/Function_Reference/add_image_size)):
 *     ```
       <?php 
   
       $galleryarray = get_post_gallery_ids($post->ID);
   
       foreach ($galleryarray as $id) { 
   
       $imagesize = wp_get_attachment_image_src( $id, 'medium' );
   
       ?>
   
       <figure style="background-image: url(<?php echo $imagesize[0]; ?>);">
   
       	<a data-name="<?php echo $id; ?>" id="<?php echo $id; ?>" href="<?php the_permalink(); echo '#' . $id; ?>"></a>
   
       </figure>
   
       <?php } ?>
       ```
   
 * Sorz for spelling 😉

Viewing 10 replies - 1 through 10 (of 10 total)