INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

global when doing have_posts inside function?

global when doing have_posts inside function?

(OP)
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:

CODE

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. I know the if (The Loop) code works correctly because I've tried it right at the top of page.php.
 

RE: global when doing have_posts inside function?

(OP)
Thanks for the reply, Billy. I tried:

CODE

function F_custom_fields()
{
    global $wp_query, $post;
    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;
}

This did not work, unfortunately. I had tried each of those individually before (plus a few others), with no luck, and I thought about combining them, as well. Maybe you're on the right track, though, and it's just a matter of finding the right combo. I'll fiddle some.

RE: global when doing have_posts inside function?


Do you have another 'the loop' before your code? If so, you might need to rewind the posts first. You should perhaps rewind them at the end anyway, so give this a shot:

CODE

function F_custom_fields() {
    global $wp_query, $post;
    rewind_posts();
    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;
    rewind_posts();
}

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: global when doing have_posts inside function?


Incidentally, are $custom_fields, $page_icon, and $div_id local to that function? If so, how can you tell your code is not working it is doesn't actually do anything you could notice outside of the function?

My basic PHP knowledge doesn't stretch as far as knowing all the scoping rules smile

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: global when doing have_posts inside function?

(OP)
For all of my vaunted intelligence, there were some debugging things I should have automatically tried first and didn't. I found that my original function above works just fine. By including some echoes - seen here...

CODE

function F_custom_fields()
{
    if (have_posts()) : while (have_posts()) : the_post();
        $custom_fields = get_post_custom();
            F_pre($custom_fields, 'print', $cf);
        $page_icon = $custom_fields['eva_page_icon'][0];
        $div_id = $custom_fields['eva_div_id'][0];
        echo 'From functions.php: $page_icon = " ' . $page_icon . ' " + $div_id = " ' . $div_id . ' "<br />';
    endwhile; endif;
}

...I learn that the values are being assigned to $page_icon and $div_id correctly. The problem seems to be when I try to "global" them into my header.php file, they're not getting hooked in. Here's an example:

CODE

global $div_id;
echo "From header.php = $div_id <br />";
if (isset($div_id))
{
    echo '<div id="'.$div_id.'">'."\n";
}

So I think I'm just around the corner from solving this.

RE: global when doing have_posts inside function?

(OP)
Well, Billy, I tried globaling them inside the function, but then the echo statement I had inside the function couldn't print them. Which was particularly strange to me. I did however come up with a solution.

In functions.php:

CODE

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:

CODE

$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 in header.php):

CODE

$end_div_id = F_custom_fields('div_id');
if ($end_div_id) { echo '</div><!-- [$div_id] -->'."\n"; }

Incidentally, this all relates to the custom fields on the editing page for a WordPress Post or Page:


Thanks, Billy, for sticking with me while I batted this around!

RE: global when doing have_posts inside function?

to globalise a variable you just need to do as Dan said

so in this case add

CODE

global $div_id;

as the first line of header.php and the F_custom_fields function.  but bear in mind that div_id might well be used elsewhere by WP.  it is better to use a more esoteric naming convention for variables that you are going to bring into global scope.  such as '$mytheme_div_id' or the like.

also please note that this code

CODE

 echo '</div><!-- [$div_id] -->'."\n"; }
will NOT work the way you want it to.  Variables in php are NOT expanded within single quotes.  so you need this instead

CODE

echo "</div><!--[$div_id] -->\r\n"; } //close curly braces
instead.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close