Support » Fixing WordPress » css – dynamic class – syntex

  • Resolved tania


    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…


Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    it worked. thanks for that.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘css – dynamic class – syntex’ is closed to new replies.