Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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="" />
share|improve this question
    
Nice idea, maybe ill use that in a future theme :) –  Ole Henrik Skogstrøm Feb 5 '12 at 14:40

2 Answers 2

up vote 5 down vote accepted

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);
share|improve this answer
    
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. –  Sarah Vessels Feb 5 '12 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.

Thanks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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