WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] css - dynamic class - syntex (3 posts)

  1. tania
    Member
    Posted 8 years ago #

    Hi
    I am very new to PHP and am trying to do what I thought what would be simple...any help would be great...

    I would like a class to be dynamic and at the page top to define it. This relates to my headers.

    I have a page called page_4wd.php and put the following:

    <?php
    /*
    Template Name: 4wd_Courses
    Description: A template for my 4wd course pages.
    */
    $headerBG = "4wd";
    ?>
    <?php get_header(); ?>

    on my header page i have this:

    <div id="header" class="<?php echo $headerBg; ?>">

    and in my css this:
    #header .whatsnew{background-image: url(images/header_4wd.jpg);}

    I though that this way - i would not have to change as many files for my different pages (with different headers)...

    Well, obviously, my stuff is way out as all I get is this in the source: <div id="header" class="">

    What am I doing wrong...

    Cheers
    Tania

  2. Ethilien
    Member
    Posted 8 years ago #

    The problem here is with variable scope. When you call get_header() you are entering a function, which has local scope, so you cannot access the variable $headerBG which you set ealier. AFAIK there really is no easy way to do what you are attempting.

    I suppose you could try making $headerBG global...
    global $headerBG;
    $headerBG = "4wd";

    then in the header file:
    <div id="header" class="<?php global $headerBG; echo $headerBG; ?>">

    That should probably work.

  3. tania
    Member
    Posted 8 years ago #

    it worked. thanks for that.
    tania

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.