Title: global when doing have_posts inside function?
Last modified: August 19, 2016

---

# global when doing have_posts inside function?

 *  Resolved [marktj](https://wordpress.org/support/users/marktj/)
 * (@marktj)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/global-when-doing-have_posts-inside-function/)
 * I need to access some custom field info before doing get_header() in my various
   templates (e.g., home.php, page.php, archive.php, etc.), so I’ve set up the following
   in functions.php:
 *     ```
       function F_custom_fields() {
       	if (have_posts()) : while (have_posts()) : the_post();
       		$custom_fields = get_post_custom();
       		$page_icon = $custom_fields['eva_page_icon'][0];
       		$div_id = $custom_fields['eva_div_id'][0];
       	endwhile; endif;
       }
       ```
   
 * Since this is not working, I figure there is a variable/array/other that I need
   to set with **global $var** as the first thing in the function. I just can’t 
   figure out what it is.

Viewing 1 replies (of 1 total)

 *  Thread Starter [marktj](https://wordpress.org/support/users/marktj/)
 * (@marktj)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/global-when-doing-have_posts-inside-function/#post-1100180)
 * I ended up having to go about this another (better) way.
 * In functions.php:
 *     ```
       function F_custom_fields($need)
       {
               // The Loop all on one line
           if (have_posts()) : while (have_posts()) : the_post(); endwhile; endif;
               // Get all of the custom items from the wp_postmeta table for this post/page
           $custom_fields = get_post_custom();
               // Return the needed custom item, if it exists
           if ($need == 'page_icon')
           {
               $page_icon = $custom_fields['eva_page_icon'][0];
               if (!empty($page_icon))
               {
                   return $page_icon;
               }
               else
               {
                   return FALSE;
               }
           }
           else if ($need == 'div_id')
           {
               $div_id = $custom_fields['eva_div_id'][0];
               if (!empty($div_id))
               {
                   return $div_id;
               }
               else
               {
                   return FALSE;
               }
           }
       }
       ```
   
 * In header.php:
 *     ```
       $begin_div_id = F_custom_fields('div_id');
       if ($begin_div_id)
       {
           echo '<div id="'.$begin_div_id.'">'."\n";
       }
       ```
   
 * And in footer.php (to close the div tag started in header.php:
 *     ```
       $end_div_id = F_custom_fields('div_id');
       if ($end_div_id)
       {
           echo '</div>';
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘global when doing have_posts inside function?’ is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [get_post_custom](https://wordpress.org/support/topic-tag/get_post_custom/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [have_posts()](https://wordpress.org/support/topic-tag/have_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [marktj](https://wordpress.org/support/users/marktj/)
 * Last activity: [16 years, 11 months ago](https://wordpress.org/support/topic/global-when-doing-have_posts-inside-function/#post-1100180)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
