• Resolved marktj

    (@marktj)


    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

    (@marktj)

    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.