Im trying to get this featured image only when wiewport is < 768:
add_image_size( 'img-movil', 660, false );
I have no much coding skills. I was reading about how to pass a variable from JS to PHP with jQuery $.ajax()
but I don’t understand it completely. This is what I did:
First step. I try to set JS variable and send it to the server:
$(document).ready(function(){
var viewportWidth = $(window).width();
if (viewportWidth < 768) {
var modoView = 'movil';
$.ajax({
type: "POST",
url: 'front-page.php',
data: {
modoView : modoView
},
success: function(data) {
alert("success!");
}
});
}
It doesn’t work (success allert doesn’t fire). I will appreciate any help with it.
Second step (when first step works), check if PHP variable is present (this code is in the body of front-page.php):
<?php
if (isset($_POST['modoView'])) {
$my_image_size="img-movil";
}
?>
Is it right?
Thank you in advance.