Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Created April 8, 2011 05:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattwiebe/909331 to your computer and use it in GitHub Desktop.
Save mattwiebe/909331 to your computer and use it in GitHub Desktop.
Conditional <html> for WordPress
<?php
/**
* The WordPress answer to:
* @link paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
* @author Matt Wiebe
* @link http://somadesign.ca/
* @var $min int minimum version of IE to target.
* @var $max int maximum version of IE to target - will receive no IE-specific classes.
*/
function sd_conditional_html_elements( $min = 7, $max = 9 ) {
$i = $min;
ob_start();
language_attributes();
$attr = ob_get_clean();
$html = '<html class="no-js%s" '. $attr .'>';
$comments = '<!--[if %s %s]> %s <![endif]-->'."\n";
while ( $i < $max ) { $j = $i; //cache for second while loop
$c = array( 'ie'.$i );
$ie = ( $i !== $min ) ? 'IE' : 'lte IE';
while ( $j < $max ) { $j++;
$c[] = 'ie-lt'.$j;
}
$classes = ' '.implode( ' ', $c );
printf( $comments, $ie, $i, sprintf( $html, $classes ) );
$i ++;
}
printf( "<!--[if (gte IE %s)|!(IE)]><!--> %s <!--<![endif]-->\n", $max, sprintf($html,'') );
}
@dbernar1
Copy link

dbernar1 commented Apr 8, 2011

It actually does not seem great to me, because it is super hard to read.

I would drop most PHP out of it, and just use as close to plain HTML as possible. Alternatively I might refactor it to a point where it reads like english as much as possible. So, basically, there would be no $j and $i, etc.

@mattwiebe
Copy link
Author

I wouldn't use this to teach someone PHP, but it does the job, so that's enough for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment