WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] css – dynamic class – syntex

[Resolved] css – dynamic class – syntex

  • 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

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

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