How to determine which custom header image is being shown

I’m using a customized version of the Twenty Eleven theme. I want to set a class on the body based on which random header image is displayed. Is there some function that allows me to get the name of the custom header image that will be displayed? My goal is to style the text / background / colors based on the image being displayed.

Edit: I used Brian’s answer, placing the $header_image code before the <body> tag, and also made some changes in header.php. I had to comment out the existing call to get_header_image():

Read More
// Check to see if the header image has been removed
//$header_image = get_header_image();

I also changed the header img tag to use the $header_image variable instead of using a call to header_image() to get the file path:

<img src="<?php /*header_image();*/ echo $header_image; ?>"
    width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>"
    alt="" />

Related posts

Leave a Reply

2 comments

  1. For the TwentyEleven theme, you can place this above your BODY tag in header.php. The theme will recognize that $header_image is already set, so no other mods would be necessary.

    $header_image = get_header_image();
    $image = basename($header_image);
    $image = explode('.',$image);
    $class = 'header-image-'.$image[0];
    

    Add the $class parameter to the body_class function to insert your header image class.

    body_class($class);
    
  2. Alright, so after a lot of digging, I was able to find that the line in the NEW theme was inside of the lib/structure/header.php file. Not sure it this will be translatable to anyone else or not, but I’m hoping so.

    Thanks