WordPress.org

Ready to get started?Download WordPress

Forums

OT: WordPress site: Are you using s script for hea (9 posts)

  1. KarlB
    Member
    Posted 10 years ago #

    Hi Guys,
    I woondered whether you are using a script to change your website headers for certain dates or are you doing this manually?
    If dynamically can you point me in the right direction for a similar script?
    Thanks
    Karl

  2. Lester Chan
    Member
    Posted 10 years ago #

    if i am not wrong s maunally

  3. tcervo
    Member
    Posted 10 years ago #

    I'm using a script to change my header graphic randomly each time you reload the page (or visit another page/post.)
    Here's what I do:
    (I'm going to step through this backward...)
    In my index.php file, right after I load my main stylesheet, I load the header style sheet as a PHP file. (Thanks to Dunstan Orchard at 1976design for the tip on using a dynamic PHP file as a style sheet.) This sheet contains ONLY the header declaration (be sure to take that part out of your main style sheet...) Here's how I call the header style sheet:
    <style type="text/css" media="screen">@import "<?php echo $siteurl; ?>/tc-header-css.php";</style>
    (Notice the .php extention.)
    Now, let's have a look at the tc-header-css.php file itself:
    The first thing is to declare the output as CSS (thanks again to Dunstan):
    <?php // declare the output of the file as CSS
    header('Content-type: text/css');
    ?>
    Next (and last) is the style declaration that includes a call to a randomize function:
    div#header { height: 123px; background: url(images/headers/<?php include $abspath.'images/headers/randomimage.php'; ?>) no-repeat 0 0; }
    That's it for the header style sheet. Now, in my /images/headers/ directory, I drop in any graphics I'd like to use as header graphics, along with the radomimage.php file.
    randomimage.php is fairly straightforward:
    <?php
    $path = "images/headers/";
    $handle = opendir($path);
    while (false !== ($file = readdir($handle))) {
    if(ereg(".jpg$|.gif$", $file)) {
    $filelist[] = $file;
    $count = count($filelist);
    }
    }
    closedir($handle);
    $arraynum = $count-1;
    $randnum=mt_rand(0,$arraynum);
    ?>
    <?php echo "$filelist[$randnum]"; ?>
    That's it!
    You can see it in action at http://www.tcervo.com
    -Tony

  4. tcervo
    Member
    Posted 10 years ago #

    Oh, I just noticed the "on certain dates" part of the original question. You could use the same basic idea (load the image dynaically), but instead of calling randomimage.php, call a different script. This could simply compare the current date against a list (or array?) of predetermined "special" days, then load the appropriate graphic...otherwise it would load a default image.
    -Tony

  5. KarlB
    Member
    Posted 10 years ago #

    Hi Tony,
    That is a great help, I have worked out a date script and then I realised that my header image was in a css style as a background image. The way you have explained will work wonders.
    I had been looking at Duntuns site before I read your post:)
    Thanks
    Karl

  6. Zaaba
    Member
    Posted 10 years ago #

    I've discovered a much easier method to do the above and all it takes is naming the images-to-be-randomized in a specific manner.
    First, you call the "dynamic header" CSS file like so:
    <style type="text/css" media="screen">@import "<?php echo $siteurl; ?>/css/thedynamicheader.php";</style>
    Then in thedynamicheader.php:
    <?php // declare the output of the file as CSS
    header('Content-type: text/css');
    ?>
    #header h2 {
    text-indent: -5000px;
    background: url(../img/headers/<?php echo rand(1,4) ?>.gif) no-repeat;
    height: 180px;
    width: 600px;
    margin: 0;
    padding: 0;
    }
    What is essential here is that the background element points to the folder where you've stored the images-to-be-randomized.
    The <?php echo rand(1,4) ?> outputs a number from 1 to 4 which results in a random gif e.g. 1.gif, 4.gif and so on.
    Adding images to this list will just need a small edit to increase the random number generated to suit the number of incrementally numbered files in the directory.
    I realise this method is very rudimentary, but I could not get the script above by Tony to work hence me looking for a simpler solution. What I was originally looking for was a script to echo a random NAME of the file e.g. todaysbreakfastmenu.png and thus incorporate that output via a php include in the dynamicheader css/php hybrid.
    Can someone help?

  7. Anonymous
    Unregistered
    Posted 10 years ago #

    tcervo: Sometimes I get criticised for criticising. Well I just wanted to say that for my money you are an asset to this forum. I always enjoy reading your posts and I learn a lot of stuff. You have a nice concise user friendly style and make the technical sound accessible. Whenever there is stuff which is likely to be useful to a lot of folks you are there with a timely solution either complete or one that can be developed. Nice work.And not a tutorial in sight. Readers: This is a guy who knows what the heck he is talking about.

  8. tcervo
    Member
    Posted 10 years ago #

    Zaaba,
    What types of problems were you encountering with the example I posted? If we could solve whatever issues you're having, it sounds like exactly what you want: it'll return a random image NAME. You don't have to alter any code...just drop a new image into the directory and it's immediately available to the script.
    Which part isn't working?
    Anon: thanks...
    -Tony

  9. Zaaba
    Member
    Posted 10 years ago #

    @Tony:
    Hey it does work! Whoopy! All it took was to change
    $path = "images/headers/";
    to
    $path = ".";
    since I was dumping the file in the same directory as the pictures.
    I then call the image with the css/php hybrid file by using:
    #test {
    background: url(../img/headers/<?php include $abspath.'img/headers/randomimage.php'; ?> no-repeat)
    }
    Paths just confuse me thats just it. It doesn't help that my wordpress installation is sitting on one directory higher than my root directory!
    What exactly does $abspath. output? And how does this differ from $siteurl?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.