• Skip to primary navigation
  • Skip to main content
  • Skip to footer

JH Tech Services

A knowledgebase

  • Portfolio
  • Knowledgebase
  • About

Adding a class to an img tag in WordPress depending on image’s orientation

June 12, 2013 by jer0dh

This does not look in exif data of the file. This only compares width and height and adds a landscape class or portrait class depending.

I’m adding a filter hook using my genesis framework. The hook is genesis_get_image. I’m editing the html for the image.

One has to test if $output is FALSE before passing it to simplexml_load_string()


add_filter('genesis_get_image', 'pb_get_image');
function pb_get_image($output){

    if ($output) {
	  $xml = simplexml_load_string($output);
	  $width = $xml["width"];
	  $height = $xml["height"];
	  ($xml["class"]) ? $class = $xml["class"] : $class = "";
      if (intval($width) >= intval($height)){
	       $class .= " landscape";
		   }
	  else {
			$class .= " portrait";
		   }
	  $xml["class"] = trim($class);
	  $output = $xml->asXML();
	  }
	return $output;
}

  • Genesis
  • Knowledgebase
  • PHP

Footer

  • Portfolio
  • Knowledgebase
  • About

© 2023