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():

// 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="" />
  • Nice idea, maybe ill use that in a future theme :) Commented Feb 5, 2012 at 14:40

2 Answers 2


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.

  • Thanks! This was almost correct for the Twenty Eleven theme. I had to change a couple other lines in header.php--see my edited question for details. Commented Feb 5, 2012 at 20:00

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.


Your Answer

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

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