change header image on refresh

  • cheatcut


    is there an option in the theme to do this?

    if not, is there any simple code that can accomplish this?



    I hacked in some code to do this for my site. Created a child-theme first so I can edit theme files without loosing changes when Syahir updates his files. Created a folder with my logo .jpgs cut to 900×198 (or whatever your current header size is). Naming convention for all my logos are “name.N.jpg” where N = N+1. logo.1.jpg, logo.2.jpg, etc.

    Opened up my child header file and made the following changes:

    } else {
    /* RT: Custom header code for random images by page ID */
    /* $header_img = get_header_image(); */

    if ( is_page('19') ) {
    $header_img = "hike." . rand(1,7) . ".jpg";
    elseif ( is_page('30') ) {
    $header_img = "archives." . rand(1,1) . ".jpg";
    elseif ( is_page('359') ) {
    $header_img = "gators." . rand(1,2) .".jpg";
    else {
    switch (date('md')){
    case "1031":
    $header_img = "logo.1.jpg";
    case "1122":
    $header_img = "logo.1.jpg";
    case "0214":
    $header_img = "logo.1.jpg";
    $header_img = "logo." . rand(1,35) . ".jpg";

    Then I made a change to:

    <!-- RT: Added custom path to background:url -->
    <div id="header" style="background-image:url(/images/logo_graphene/<?php echo $header_img; ?>);">
    <!-- RT: End custom code -->

    In a nut shell: for each page ID I declare, use a random .jpg for my header image each time you refresh the page. You can specify an image for a certain date, etc. It does require a bit of setup with your .jpgs and you have to make sure you insert your logo folder path into the div id statement. View your page source to debug.

    I’m sure there are easier ways to do this but it works if you want custom header image rotation independent of post/page images.

    Hope this helps.




    where did you put this code?

    <!– RT: Added custom path to background:url –>
    <div id=”header” style=”background-image:url(/images/logo_graphene/<?php echo $header_img; ?>);”>
    <!– RT: End custom code –> I have to declare each page id?

    here’s my site for example:




    Each page, post, comment, etc. gets a unique ID within WP that can be called from PHP functions like is_page(), etc. You don’t have to do anything other than declare the right ID number.

    The second half of the code change is edited in the same header.php file you modify with the logo. rand() .jpg code from above. Go down about 10 lines and find the same

    <div id="header" style="background-image:url(<?php echo

    add additional folder paths and customize the $header_img part to match the IF:THEN statements.

Viewing 4 posts - 1 through 4 (of 4 total)

  • You must be logged in to reply to this topic.