Hello,
First of all, thank you for this great plugin.
I'm using wordpress, masonry and ajax. I have a problem with masonry when I use ajax to filter my posts by cat.
See example below:
That's the current display for my articles before filtering with ajax:
And after filtering with ajax, masonry doesn't work:
Here is my codes:
I have a my-script.js file for masonry with:
jQuery(window).load(function(){
jQuery('.sub-home-article-container').masonry({
columnWidth: '.grid-sizer',
gutter: '.gutter-sizer',
itemSelector: '.item',
percentPosition: true
});
});
Then that's my file who is for my ajax call, change_cat_ajax.js:
jQuery(document).ready(function($){
$('#cat-home-select').change(function(){
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
var selected_cat = $(this).val();
console.log(selected_cat);
$('.tagged-posts').fadeOut();
data = {
action: 'filter_posts', // function to execute
my_ajax_nonce: my_ajax_vars.my_ajax_nonce, // wp_nonce
taxonomy: selected_cat, // selected tag
};
$.post( my_ajax_vars.my_ajax_ajax_url, data, function(response) {
if( response ) {
// Display posts on page
$('.tagged-posts').html( response );
// Restore div visibility
$('.tagged-posts').fadeIn()
};
});
});
});
And in my function.php I have the code below who is called:
// Script for getting posts
function ajax_filter_get_posts( $taxonomy ) {
// Verify nonce
if( !isset( $_POST['my_ajax_nonce'] ) || !wp_verify_nonce( $_POST['my_ajax_nonce'], 'my_ajax_nonce' ) )
die('Permission denied');
$taxonomy = $_POST['taxonomy'];
// WP Query
$args = array(
'category_name' => $taxonomy,
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'n_de_ldition',
'value' => 36,
)
)
);
// If taxonomy is not set, remove key from array and get all posts
if( !$taxonomy ) {
unset( $args['category_name'] );
}
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
// My content
No posts found
die();
}
add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
So, I think I have to reload masonry after each call of ajax but I don't know how and where.
Do someone have an idea?
Any help would be great. Thank's in advance.
Best regards
Dan