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

  1. tania
    Posted 10 years ago #

    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:

    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...


  2. Ethilien
    Posted 10 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
    Posted 10 years ago #

    it worked. thanks for that.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.