title: Variable outside foreach loop php
body: <p>I got a problem.</p>
  
  <p>On Wordpress I define a foreach loop, to get some informations from attached image like title, description, etc...</p>
  
  <p>Outside this loop, I want to call the result of various variables. How can I get <code>$attachment_description</code>, <code>$attachment_caption</code> and <code>$attachment_title</code> outside my loop?</p>
  
  <p>Thanks for your help. Below the code : </p>
  
  <pre><code>if ( $attachments ) : 
  
  echo $before;
      foreach( $attachments as $ID ){
          $attachment = get_post($ID);
          $attachment_description = $attachment-&gt;post_content; //Display description
          $attachment_caption = $attachment-&gt;post_excerpt; //Display caption
          $attachment_title = $attachment-&gt;post_title; //Display title
  
          //IF : Affichage sur certaines images
          //if($attachment_description != ''){
          //echo '&lt;div class="item"&gt;' . wp_get_attachment_image( $attachment-&gt;ID, 'portfolioslider' ) . '&lt;div class="captionportfolio"&gt;&lt;h1 class="titrecaption"&gt;' . $attachment_title . '&lt;/h1&gt;' . $attachment_description . '&lt;/div&gt;&lt;/div&gt;';
          //}// else {
          echo '&lt;div class="item"&gt;' . wp_get_attachment_image( $attachment-&gt;ID, 'portfolioslider' ) . '&lt;/div&gt;';
          //}
      }
  echo $after;
  
  endif;
  </code></pre>
  
  <p>Nothings seems to works, this is the first file with the loop : </p>
  
  <pre><code>&lt;?php 
  /**
   * Make sure we can access post data easily.
   */
  global $post;
  
  /**
   * Setup variables needed for the gallery
   */
  $attachments = get_post_meta( $post-&gt;ID, '_ebor_portfolio_gallery_list', true );
  $type = get_post_meta( $post-&gt;ID, '_ebor_gallery_format', true );
  $before = '&lt;div class="owl-carousel portfolio-slider custom-controls"&gt;';
  $after = '&lt;/div&gt;';
  
  if( $type == 'carousel' ){
  $before = '&lt;div class="carousel-gallery-wrapper"&gt;&lt;div class="carousel-gallery custom-controls"&gt;';
  $after = '&lt;/div&gt;&lt;/div&gt;';
  }
  
  /**
   * If we found items, output the gallery.
    * $before and $after change depending on the gallery chosen.
   */
  if ( $attachments ) : 
  
  echo $before;
  
  $arrayInformation = array();
  
      foreach( $attachments as $ID ){
          $attachment = get_post($ID);
          $attachment_description = $attachment-&gt;post_content; //Afficher la description
          $attachment_caption = $attachment-&gt;post_excerpt; //Afficher le caption
          $attachment_title = $attachment-&gt;post_title; //Afficher le titre
  
          $arrayInformation[$ID] = array("title" =&gt;$attachment_title, "caption" =&gt;$attachment_caption, "description" =&gt;$attachment_description);
  
          //IF : Affichage sur certaines images
          //if($attachment_description != ''){
          //echo '&lt;div class="item"&gt;' . wp_get_attachment_image( $attachment-&gt;ID, 'portfolioslider' ) . '&lt;div class="captionportfolio"&gt;&lt;h1 class="titrecaption"&gt;' . $attachment_title . '&lt;/h1&gt;' . $attachment_description . '&lt;/div&gt;&lt;/div&gt;';
          //}// else {
          echo '&lt;div class="item"&gt;' . wp_get_attachment_image( $attachment-&gt;ID, 'portfolioslider' ) . '&lt;/div&gt;';
          //}
      }
  echo $after;
  
  endif;
  </code></pre>
  
  <p>And I want to call the content of the variable into this file : </p>
  
  <pre><code>&lt;div class="container inner"&gt;
  
  &lt;?php get_template_part('postformats/format', get_post_format()); ?&gt;
  
  &lt;div class="divide30"&gt;&lt;/div&gt;
  
  &lt;?php 
  
  global $post;
  
  $titrecat = get_the_title();
  if( has_term( 'creation', 'portfolio-category' ) ) {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Création - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;'; 
  print $arrayInformation[$ID]["description"];
  }
  else if( has_term( 'realisation', 'portfolio-category' ) ) {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Réalisations - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;';
  print $arrayInformation[$ID]["description"]; 
  }
  else if( has_term( 'inspiration', 'portfolio-category' ) ) {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Inspiration - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;'; 
  print $arrayInformation[$ID]["description"];
  }
  ?&gt;  
  &lt;/div&gt;
  </code></pre>
  
  <p>get_template_part('postformats/format', get_post_format()); refer to the first file of course</p>
  
  <p>I can't put the $ID number into the second file, cause is variable, thanks for your precious help :)</p>
  
  <p><strong>UPDATE</strong>
      </p>
  
  <pre><code>&lt;?php 
  /**
  * Make sure we can access post data easily.
  */
  global $post;
  
  /**
  * Setup variables needed for the gallery
  */
  $attachments = get_post_meta( $post-&gt;ID, '_ebor_portfolio_gallery_list', true );
  $type = get_post_meta( $post-&gt;ID, '_ebor_gallery_format', true );
  $before = '&lt;div class="owl-carousel portfolio-slider custom-controls"&gt;';
  $after = '&lt;/div&gt;';
  
  if( $type == 'carousel' ){
  $before = '&lt;div class="carousel-gallery-wrapper"&gt;&lt;div class="carousel-gallery custom-controls"&gt;';
  $after = '&lt;/div&gt;&lt;/div&gt;';
  }
  
  /**
   * If we found items, output the gallery.
    * $before and $after change depending on the gallery chosen.
   */
  if ( $attachments ) : 
  
  echo $before;
  
  $arrayInformation = array();
  
      foreach( $attachments as $ID ){
          $attachment = get_post($ID);
          $attachment_description = $attachment-&gt;post_content; //Afficher la description
          $attachment_caption = $attachment-&gt;post_excerpt; //Afficher le caption
          $attachment_title = $attachment-&gt;post_title; //Afficher le titre
  
          $arrayInformation[$ID] = array("title" =&gt;$attachment_title, "caption"       
  =&gt;$attachment_caption, "description" =&gt;$attachment_description);
  
          echo '&lt;div class="item"&gt;' . wp_get_attachment_image( $attachment-&gt;ID, 'portfolioslider' )   
  . '&lt;/div&gt;';
      }
  echo $after;
  
  endif;
  ?&gt;
  
  &lt;div class="divide30"&gt;&lt;/div&gt;
  
  &lt;?php 
  
  $idimage = $attachment-&gt;ID;
  
  $titrecat = get_the_title();
  if( has_term( 'creation', 'portfolio-category' ) )  {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Création - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;'; 
  print $arrayInformation[$idimage]["description"];
  }
  else if( has_term( 'realisation', 'portfolio-category' ) ) {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Réalisations - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;'; 
  }
  else if( has_term( 'inspiration', 'portfolio-category' ) ) {
  echo '&lt;p&gt;&lt;span class="portfolio-title"&gt;Inspiration - &lt;/span&gt;' . $titrecat . '&lt;/p&gt;'; 
  }
  ?&gt;
  
  
  &lt;/div&gt;
  </code></pre>