Responsive image markup
-
Hi,
I’m a php developer and started my first WP theme 4 weeks ago to get an understanding of the WP-way to do things. My first goal is to build a perfect match of Twentyfourteen based on Bootstrap 3.2.0. markup and styles. Until now, every problem could be solved. I now have multiple widget areas, navigations, responsive post thumbnails and featured content – all speaking “Bootstrap”.
The only problem left are image attachments. There is more than one way to get images to be responsive, and I want to start with the one, which can live with image dimensions generated by WP. To get this working there must be a wrapper element for the image with knowledge of the image dimensions. So the following markup will work, if “size-medium” reflects the percentage of the available image width in relation to the content width (I added it as an inline style for showcasing):
<span class="size-medium" style="width:36.6%;"> <img height="219" width="300" alt="The ALT text" src="myimage.jpg" class="img-responsive"> </span>
or
<a href="myBigImage.jpg" class="size-medium" style="width:36.6%;"> <img height="219" width="300" alt="The ALT text" src="myimage.jpg" class="img-responsive"> </a>
or
<figure class="wp-caption size-medium" id="attachment_50" style="width:36.6%;"> <a href="myBigImg.jpg"> <img height="219" width="300" alt="The ALT text" src="myimage.jpg" class="img-responsive"> </a> <figcaption class="wp-caption-text">The Image Caption</figcaption> </figure>
My first approach was the “img_caption_shortcode” filter. The “$atts[‘width’]” can be used to determine the correct wrapper class for the image. This is a bit hacky, but works like charme – as long as you have a caption and stick to predefined image sizes. If you don’t, this filter will fail, since Core function “img_caption_shortcode” returns a static image tag in case of missing captions before applying the custom filter. This makes it impossible to wrap an img tag into “figure” tags by default (which would be perfectly ok, since “figcaption” is optional).
There would be no problem at all, if “size-medium” class could be applied to any wrapper element instead, be it a link or figure tag. But instead it is added to the img tag, where it is useless (unless somebody invents the css parent selector)
So, to ask a more abstract question: How can I create a wrapper class for an img-tag, which works in any scenario?
Thanks for any help,
Thomas
- The topic ‘Responsive image markup’ is closed to new replies.