WordPress.org

Forums

How to call PHP in your CSS: specifically changing header imgs (3 posts)

  1. tbag
    Member
    Posted 9 years ago #

    Question: How do i integrate PHP into my CSS?
    Why: I want to have a random header image, and the 2 plugins i grabbed won't work.
    -----------------------------------------------
    So far:
    in my style.css i want to edit this entry:
    -----------------------------------------------
    #headerimg {
    background: url("images/header.jpg");
    }
    -----------------------------------------------
    And i have this php to grab random images (named 1.jpg, 2.jpg) from a folder.
    -----------------------------------------------
    <?php

    // Change this to the total number of images in the folder
    $total = "2";

    // Change to the type of files to use eg. .jpg or .gif
    $file_type = ".jpg";

    //Starting the randomizing

    $start = "1";

    $random = mt_rand($start, $total);

    $image_name = $random . $file_type;

    echo $image_name;

    ?>
    -----------------------------------------------
    The images and the php will be in the same folder.

    So how do i integrate my PHP file into my CSS? Just naming it in the url tag won't work.

    any ideas?

  2. chaaban
    Member
    Posted 9 years ago #

    make a php function that will generate the css style header , you pass the info to the function and function will generate the stylle depending of what it get ..


    function php_generate_css ($image_name , $more_if_needed) {

    echo " <style>
    background: url(\"images/$image_name.jpg\");
    </style>
    ";

    }

    and when u want to call : php_generate_css ("1.jpg","more variables if needed") ;

  3. tbag
    Member
    Posted 9 years ago #

    beauty thats clever. ty.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags