So I have a range slider in the footer of my whole site:
<input id="ex1" type="range" min="0" max="60" step="1" />
<div id='mydiv'>test</div>
And on slideStop I am redirecting to my homepage:
$(document).ready(function(){
$('#ex1').on('change', function () {
if (document.location.href.indexOf('category') > -1)
{
window.location.href = 'http://localhost/wordpress/';
}
var id = $(this).val();
$.ajax({
type: "post",
dataType: "json",
url: "/wp-admin/admin-ajax.php",
data: {
action:'get_data',
id: id
},
success:function(data) {
$('#mydiv').html(data);
}
});
});
});
And I want to pass the value of my range slider into the php on my wordpress front-page. This is the code:
<?php
$query = new WP_Query( array( 'tag' => ' ***INSERT RANGE SLIDER VALUE HERE*** ' ) );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();?>
<?php the_content();?>
<?php endwhile; endif;?>
So if the range slider stops on 44, the home page would show all posts tagged ’44’.
I haven’t used AJAX before and struggling to understand where to get started.
Thanks !
3
Answers
This will output the current value of the slider after user interaction to a div.
HTML
JS (jQuery 3.4.1)
The easy way would be to pass your var ass url parameter. On your front-page, just read this parameter and set is as parameter for your query. You just have to change some parts in your files.
change your js at this part:
No magic here, just add any parameter to your url, in this example: 12.
In your php-file:
Just us a GET-Request for your url – parameter (in this example we use "slider" ).
Any questions?
Try with this code –