====== Images responsives ====== ===== Technique du cookie de résolution ===== Insérer dans functions.php : // ----------------------------------------------------------------------------- // Gestion du responsive add_action( 'wp_head', 'add_viewport_cookie_js' ); function add_viewport_cookie_js() { echo << document.cookie='resolution='+Math.max(screen.width,screen.height)+'; path=/'; JAVASCRIPT; } /* The following cookie handling code is a modified version of the Matt Wilcox's Adaptive images plugin, * which is licensed under a Creative Commons Attribution 3.0 Unported License. * Plugin : http://adaptive-images.com/ * Licence : http://creativecommons.org/licenses/by/3.0/ */ global $client_width; if(!is_admin()) { if (isset($_COOKIE['resolution'])) { $cookie_value = $_COOKIE['resolution']; // does the cookie look valid? [whole number, comma, potential floating number] if (! preg_match("/^[0-9]+[,]*[0-9\.]+$/", "$cookie_value")) { // no it doesn't look valid setcookie("resolution", "$cookie_value", time()-100); // delete the mangled cookie } else { // the cookie is valid, do stuff with it $client_width = (int) $cookie_value; } } } /*function getResponsiveImage($path, $id = "", $class = "", $alt = "", $title = "") { global $client_width; $return = ''; //return '

'.$client_width.'

'; 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'image conditionnel via champs ACF : **Attention, toujours ajouter un test avec && $client_width != null (pour correction, donc <) ou || $client_width == null (pour valeur par défaut, donc >) pour s'assurer du bon chargement de la première page.** '; } else { echo ''.$img['alt'].''; } } ?>