Title: Custom Fields error
Last modified: August 19, 2016

---

# Custom Fields error

 *  [line47](https://wordpress.org/support/users/line47/)
 * (@line47)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-error-2/)
 * I’m getting an error when I do not have any values for the ‘purchase’ custom 
   field I have created.
 * Here is the error.
    `Warning: Invalid argument supplied for foreach() in /home/
   terminal/www/www/wp-content/themes/terminaldusk/pageDiscogNew.php on line 40`
 * I know this is b/c the purchase links are null but how do i stop this error from
   showing or perhaps show some different code when there are no ‘purchase’ custom
   fields.
 *     ```
       <?php $purchaselinks = get_post_meta($post->ID, 'purchase', true);
   
       	if($purchaselinks) : ?>
                 <h3>Purchase</h3>
                 <p>If you know a place to purchase this record that is not listed <a href="http://www.terminaldusk.com/contact">please let us know</a>.</p>
                 <?php endif; ?>
                 <?php $value = get_post_meta($post->ID, 'purchase', false); ?>
                 <ul class="purchaseLinks">
                   <?php foreach($value as $value) {
   
       			if($value == 'purchase-addictech') {
       		echo '<li><a href="http://terminaldusk.addictech.com/">Addictech (Mp3)</a></li>';
       	}
       	elseif($value == 'purchase-adnoiseam') {
       		echo '<li><a href="http://www.adnoiseam.net/store/terminal-dusk_m40.html?osCsid=68c63d8a5c49d673d3f3a009e2a2a9b7">Ad Noiseam (DE)</a></li>';
       	} 
   
       	elseif($value == 'purchase-amazon') {
       		echo '<li><a href="http://www.amazon.com/s/qid=1245201707/ref=sr_f3_all?ie=UTF8&rs=&keywords=terminal%20dusk&rh=i%3Adigital-music%2Ck%3Aterminal%20dusk%2Ci%3Adigital-music-album">Amazon.com (Mp3)</a></li>';
       	}
       ```
   

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-error-2/#post-1143327)
 * Just eyeing this, without testing, something like this:
 *     ```
       <?php
       $purchaselinks = get_post_meta($post->ID, 'purchase', true);
       if($purchaselinks) { ?>
         <h3>Purchase</h3>
         <p>If you know a place to purchase this record that is not listed <a href="http://www.terminaldusk.com/contact">please let us know</a>.</p>
         <ul class="purchaseLinks">
         <?php foreach($purchaselinks as $value) {
           if($value == 'purchase-addictech') {
             echo '<li><a href="http://terminaldusk.addictech.com/">Addictech (Mp3)</a></li>';
           } elseif($value == 'purchase-adnoiseam') {
             echo '<li><a href="http://www.adnoiseam.net/store/terminal-dusk_m40.html?osCsid=68c63d8a5c49d673d3f3a009e2a2a9b7">Ad Noiseam (DE)</a></li>';
           } elseif($value == 'purchase-amazon') {
             echo '<li><a href="http://www.amazon.com/s/qid=1245201707/ref=sr_f3_all?ie=UTF8&rs=&keywords=terminal%20dusk&rh=i%3Adigital-music%2Ck%3Aterminal%20dusk%2Ci%3Adigital-music-album">Amazon.com (Mp3)</a></li>';
           } // if($value ==
         } // foreach($purchaselinks
       } // if($purchaselinks)
       ?>
       ```
   
 *  Thread Starter [line47](https://wordpress.org/support/users/line47/)
 * (@line47)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/custom-fields-error-2/#post-1143444)
 * Thanks! I got it working now! Cheers!

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

The topic ‘Custom Fields error’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [line47](https://wordpress.org/support/users/line47/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/custom-fields-error-2/#post-1143444)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
