masonry ajax 没用,Reload Masonry after ajax call

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:

da33875e10ccc49df9cbda04b6c20fd8.png

And after filtering with ajax, masonry doesn't work:

8190c5b825ed64535c25aac1073d9528.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值