1

I'd like to have my header banner change every day, chosen (e.g. randomly) from a list/folder of images.

So far I only found plugins that select a random banner per post or per page view, neither is what I want. Do you know of a plugin, or any other way?

Edit: Example. I'm adding a folder to my wordpress installation. This folder contains some header images. Every 24 hours, an random image is chosen from this folder and becomes the header image for 24 hours, until another random image is chosen.

Actually, I don't care how exactly the image is chosen - I'm fine with rotating through all images in the folder as well. The only thing I care about is that the chosen image stays there for 24 hours.

Basically it's the same as if I were picking a new header image manually every day.

6
  • 1
    So do you want 1) a different image each day, or 2) a different and random image each day? A different as well as random image each day is somewhat of a contradiction. Dec 12, 2010 at 22:06
  • @song: A random image.
    – mafu
    Dec 13, 2010 at 10:28
  • See answer below. Dec 13, 2010 at 13:18
  • Btw, just to make sure, I'm talking about self-hosted.
    – mafu
    Dec 29, 2010 at 21:55
  • @songdogtech Oops, I completely misunderstood - I actually meant the opposite, your option 1. Sorry!
    – mafu
    Dec 29, 2010 at 21:57

5 Answers 5

1
 <div id="header">
    <?php
        mt_srand((int)date('z'));
        $headers=glob(PATH_TO_FILES .'/*.jpg');

        $header=mt_rand(0,count($headers)-1  );
        ?>
            <img id="header-image"  `src="<?php echo URL_TO_FILES . '/'.basename($headers[$header]);  ?>" alt="header" />




 </div>

replace your current header with this. I will of course help you implement this further if you need help. (or if you need the code explained)

2
  • Actually I'd be glad if you could expand on this a little. I'm not familiar with the PHP functions you used, and I'm also not certain how I "replace the current header".
    – mafu
    Dec 29, 2010 at 21:54
  • The code does the following: it scans a directory searching for all jpg files ($headers=glob(PATH_TO_FILES .'/*.jpg');). It creates a random number (based on the day in the year), which is in the range between 0 and how many files it found. Then it displays the randomly selected file as an image. As for how you replace the current header, you'll have to search your current theme for the code that generates the header image (probably in header.php).Be careful that PATH_TO_FILES has to be a local address, telling where the files are on the server.URL_TO_FILES is the "internet link" to the dir.
    – Cronco
    Dec 30, 2010 at 1:25
1

If you can't code, sorry no.

If you can code, look at which hook these plugins are using, and create a new plugin accordingly.

1

See http://www.alistapart.com/articles/betterrotator/ I use it myself on a few sites.

2
  • It looks like this changes per page reload instead of per day? I was probably unclear in my description - I meant to have the image change once a day.
    – mafu
    Dec 29, 2010 at 21:51
  • <clarity>You're still unclear.</clarity> You want the image to change once a day, but also be a random image? And what time is the change? At midnight? Or do you want a monday.jpg, tuesday,jpg, etc, image change? Dec 29, 2010 at 23:14
0

You can take a look in this Javascript example, it's very easy to implement it in your theme.

1
  • I'd prefer to have it server-side only.
    – mafu
    Dec 29, 2010 at 21:50
0

Based on Cronco's answer and a lot of experimentation (I'm neither familiar with PHP nor with the inner workings of wordpress itself) and some cleanup, I found a solution that perfectly satisfies my needs.

Using the theme TwentyTen, I edited its header.php like this:

<?php
    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
    if ( is_singular() &&
            has_post_thumbnail( $post->ID ) &&
            ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&
            $image[1] >= HEADER_IMAGE_WIDTH ) :
        // Houston, we have a new header image!
        echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else : ?>

        <!-- patch mafu begin -->
        <?php
        $headers = glob( get_template_directory() . '/banners/*.jpg' );
        $header = (int)date('z') % count($headers);
        ?>
        <img src="<?php echo get_template_directory_uri() . '/banners/' . basename($headers[$header]); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
        <!-- patch mafu end -->

    <?php endif; ?>

If no specific image is set for the displayed page (this is specific to this theme), then a header image is chosen based on the current day from all jpg images in the theme_directory/banners folder, which has to be created.

I tried to randomize the image order more, but apparently this is not possible in an easy way, so I went with fixed order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.