Hello,
I would like to change the query-post or WP-Query loop arguments with ajax. I made a categories menu on the index.php. By defaut all categories are displaying and on the click I would like to reset the query and filter them one by one. I start with this exemple :
http://web-profile.com.ua/wordpress/dev/ajax-in-wordpress/
This is the PHP code for the index.php
Actually, the value of each input is correctly returned with : alert(data);
<div class="filter_home">
<span>Filtrer</span>
// onclick take the input value and change the loop with new arguments
<a href="#all">
<input name="input_filter" type="hidden" id="input_all" value="news,articles,divers" />Tout</a>
<a class="fnews" href="#news"><input name="input_filter" type="hidden" id="input_news" value="news" />News</a>
<a class="fdossiers" href="#tests"><input name="input_filter" type="hidden" id="input_dossiers" value="dossiers" />Dossiers</a>
</div>
<?php
// the variable to modify is $filter
query_posts('posts_per_page=10&category_name='.$filter.'&orderby&paged='.get_query_var('paged'));
if (have_posts()) : while (have_posts()) : the_post();
?>
<div class="gd_bloc_content">
<?php gd_get_image1 (120,80); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php the_excerpt (240) ; ?>
</div>
<?php endwhile; endif; ?>
the jQuery/Ajax in script.js
jQuery(document).ready(function($) {
$('.filter_home a').click(function (e) {
e.preventDefault();
$.post(ajax_object.ajaxurl, {
action: 'ajax_action',
inputs: $(this).find('input:hidden').val()
}, function(data) {
alert(data);
result = data;
return result;
});
});
});
the PHP in function.php
wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri().'/libs/js/script.js', array('jquery'), 1.0 );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action( 'wp_ajax_ajax_action', 'change_filter_post_cat' );
add_action( 'wp_ajax_nopriv_ajax_action', 'change_filter_post_cat' );
function change_filter_post_cat() {
$filter = $_POST['inputs'];
echo $filter;
die();
exit;
}
I think I need JSON to pass Jquery variable (data) to the query-post, but I don't understand where to make the junction between query-post and the variable ? Is there someone to help me ?
Thanks in advance