Variables not working in get_header (3 posts)

  1. DeadSeraphim
    Posted 10 years ago #

    In a theme I'm currently working on, before get_header() I've been putting a variable to change the stylesheet and title of the page (at the moment, $category). Inside header.php, to use $category, I have it set to echo $category at the stylesheet and title tags... except when it echos $category it doesn't output anything. The variable is blank.

    As an experiment, to make sure the variable wasn't being completely wiped, I echoed the $category variable directly after get_header() and it worked fine. I also tried directly grabbing header.php with include() and it worked fine in that case too. It's just not working at all for get_header(). Does anyone have a fix or way to resolve this without just including header.php manually?

  2. jgclarke
    Posted 9 years ago #

    Yep, I'm having this same problem as well. Anybody yet figure out a way around this?

  3. Kafkaesqui

    Posted 9 years ago #

    You want to scope your variables to global if using them across templates (or functions, or what have you).

    For those new to PHP, what I mean by "scope to global" is to define the variable as a global one before you assign it a value, or make use of it in any way:

    global $myvar;
    $myvar = 'This is a global variable';


    global $myvar;
    echo $myvar;

Topic Closed

This topic has been closed to new replies.

About this Topic