Ci-dessous, les différences entre deux révisions de la page.
| webdev:wordpress:images_responsive [03/04/2015 01:03] – créée dolo | webdev:wordpress:images_responsive [16/01/2016 21:23] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Images responsives ====== | ||
| + | ===== Technique du cookie de résolution ===== | ||
| + | Insérer dans functions.php : | ||
| + | < | ||
| + | // ----------------------------------------------------------------------------- | ||
| + | // Gestion du responsive | ||
| + | add_action( ' | ||
| + | function add_viewport_cookie_js() { | ||
| + | echo <<< | ||
| + | < | ||
| + | document.cookie=' | ||
| + | </ | ||
| + | JAVASCRIPT; | ||
| + | } | ||
| + | |||
| + | /* The following cookie handling code is a modified version of the Matt Wilcox' | ||
| + | * which is licensed under a Creative Commons Attribution 3.0 Unported License. | ||
| + | * Plugin : http:// | ||
| + | * Licence : http:// | ||
| + | */ | ||
| + | |||
| + | global $client_width; | ||
| + | if(!is_admin()) | ||
| + | { | ||
| + | if (isset($_COOKIE[' | ||
| + | $cookie_value = $_COOKIE[' | ||
| + | // does the cookie look valid? [whole number, comma, potential floating number] | ||
| + | if (! preg_match("/ | ||
| + | setcookie(" | ||
| + | } | ||
| + | else { // the cookie is valid, do stuff with it | ||
| + | $client_width = (int) $cookie_value; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /*function getResponsiveImage($path, | ||
| + | { | ||
| + | global $client_width; | ||
| + | $return = '< | ||
| + | if($id != "" | ||
| + | if($class != "" | ||
| + | if($title != "" | ||
| + | | ||
| + | $src = get_bloginfo(' | ||
| + | |||
| + | // On vérifie les fichiers dans l' | ||
| + | if($client_width != null) | ||
| + | { | ||
| + | if( $client_width <= 600 && is_file( $_SERVER[' | ||
| + | { | ||
| + | $src = $src.'/ | ||
| + | } | ||
| + | } | ||
| + | $src = $src.substr($path, | ||
| + | $return = $return.' | ||
| + | | ||
| + | $size = getimagesize($src); | ||
| + | $return = $return.' | ||
| + | | ||
| + | //return '< | ||
| + | return $return; | ||
| + | }*/ | ||
| + | </ | ||
| + | |||
| + | Ensuite, ajouter une nouvelle taille de miniature dans Wordpress. On peut faire ça avec du code ou avec le plugin Simple image sizes. Une fois ceci fait, voici un exemple de chargement d' | ||
| + | |||
| + | **Attention, | ||
| + | |||
| + | < | ||
| + | <?php | ||
| + | if( get_field(' | ||
| + | { | ||
| + | $img = get_field(' | ||
| + | if($client_width != null && $client_width <= 600) | ||
| + | { | ||
| + | echo '< | ||
| + | } else { | ||
| + | echo '< | ||
| + | } | ||
| + | } | ||
| + | ?> | ||
| + | </ | ||