WordPress.org

Ready to get started?Download WordPress

Forums

using variables before get_header() (6 posts)

  1. lavender
    Member
    Posted 7 years ago #

    Hello, i've been setting up a WordPress site for the first time and things've been pretty smooth so far, but now i'm stuck. I've found a few things on-line regarding this subject, but i guess it hasn't helped, 'cause i can't seem to get it.

    What i'm trying to do is get the side bar or header to use a variable declared at the beginning of the page (say index.php) that calls it. In other words, something like:

    <?php
    $myVariable = "something";
    get_header();
    ?>

    (and then obviously there is something in header.php that needs to use that variable)

    I understand that it isn't going to be as simple as what i wrote above because of the scope of the variable and all that stuff. I've been trying to use hooks or actions or whatever, but i still can't get it. I can put <?php wp_head(); ?> in my header.php and then put some html inside a function and use add_action() to add that to wp_head()... and that works fine. The HTML gets displayed in the header. But i can not get it to just set a variable. It stays empty.

    Does anyone know how i could do this? Thanks!

  2. apmeyer
    Member
    Posted 7 years ago #

    I am guessing you probably found a solution by now, but...

    I was trying to do the same thing. It wouldn't work. The work around I used was creating a function before the get_header() tag. While the content in the header wouldn't recognize variables set previously, it would make a call to a function set previously.

    So, in your case you could create a function like:

    <?php
    function myVariable(){
    return = "something";
    }
    ?>

    Then when you want to use that "variable" in the header, sidebar or footer, you could do something like:

    <?php
    if(myVariable() == "something"){
    //do stuff
    }
    ?>

    Not sure if that will help for your situation, but it works for what I needed.

  3. Dalton
    Member
    Posted 6 years ago #

    Here is a much easier way, if anyone else is looking for a solution for this.

    In index.php, or other template you're using:

    <?php global $your-variable;
    $your-variable = the-value;
    get_header(); ?>

    Then in your header:

    <?php global $your-variable;
    echo $your-variable; ?>
  4. websta
    Member
    Posted 6 years ago #

    daltonrooney, could your solution be configured to pull a different header file (header-1.php, header-2.php...) depending which category the post is in?

    I am wondering if a site has 40 categories or whatever, but a set of main categories (say, 1-8) could be set up for the different headers?

    Do you know how? Sorry, I am a newbie with PHP and am trying to figure out how to have a different color of header for the major categories... I'm confess the multiple conditions are beyond me.

  5. Dalton
    Member
    Posted 6 years ago #

    Hey Websta - you don't need to pass global variables to do that. You need to look into category templates: http://codex.wordpress.org/Category_Templates

    You can have a unique template for each category. The easy way to do it is just call a unique header file in each category template.

    You should also take a look at the "in_category" template tag. You could create a header that looks to see if the post is in a specific category and outputs the appropriate code at that time:
    http://codex.wordpress.org/Template_Tags/in_category

    That's probably the smarter way to do it. More PHP, less template files to maintain.

  6. websta
    Member
    Posted 6 years ago #

    Thanks daltonrooney. I will try that out.

Topic Closed

This topic has been closed to new replies.

About this Topic