WordPress.org

Ready to get started?Download WordPress

Forums

Variables not working in get_header (3 posts)

  1. DeadSeraphim
    Member
    Posted 8 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
    Member
    Posted 7 years ago #

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

  3. Kafkaesqui

    Posted 7 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';

    and:

    global $myvar;
    echo $myvar;

Topic Closed

This topic has been closed to new replies.

About this Topic