Take the 2-minute tour ×

I'm looking for a solution to a workaround. Right now the best workaround I've determined is to be able to add a class to an attachment on a wordpress page. At the moment, Wordpress doesn't have any easy way to simply add a class for CSS styling later.

Currently when the HTML is generated, it wraps it in a div. This is how it is generated (I think):

   $bigsrc =  wp_get_attachment_image_src( $attachment->ID, 'full'); 
   $zoom = $bigsrc[0];
   $src = wp_get_attachment_image_src( $attachment->ID, 'post-thumb'); 
   $attachmenttitle = apply_filters('the_title', $attachment->post_title); 
   $class = "media-image";

<div class="item-photo <?php echo $class;?>">

Is there a way to add a class based on the filename? I can easily rename the files "red-01.jpg" and "blue-01.jpg". I'm hoping that there is some script that could output html as:

<div class="item-photo media-image red"><img src="red-01.jpg" /></div>
<div class="item-photo media-image blue"><img src="blue-01.jpg" /><div>

I have to use the Wordpress attachment method since the script I'm using for a project already calls for attachments to the page, and is pretty sophisticated. If there was a simple class addition to Wordpress media uploads my problem would be solved. Thanks a bagillion!

I'm completely open to any solutions...as long as I can add a class to the div as above!

share|improve this question
What function are you using to display that image? –  Eric Strom Aug 23 '12 at 0:20
This might be better asked on wordpress.stackexchange.com –  Levi Morrison Aug 23 '12 at 0:26
Thanks, made an update to something I forgot to add! –  pet3r Aug 23 '12 at 0:42
add comment

2 Answers

Instead of using wp_get_attatchment_image(), use wp_get_attatchment_image_src() and fill in the values yourself. Once you have the image source, you can just do a substr_replace() to get rid of the file extension and throw that into the class.

share|improve this answer
add comment

What about

div class="item-photo <?php echo ($class." ".$src); ?>">

or $bigsrc, or whatever value is most useful in identifying the image (not sure what the values are)

share|improve this answer
add comment

Your Answer


