我有以下代码,我正在尝试使用ajax加载显示wordpress / woocommerce帖子的更多功能,但'cat'的值似乎始终保持在它获得的第一个值。
我是否需要每次或类似地删除它以便它开始持有正确的'猫'?
var ppp = 4; // Post per page
var pageNumber = 1;
$(".et-load-more").on("click", function(e) {
e.preventDefault();
var cat = $('.et-load-more').data('category');
pageNumber++;
$.ajax({
cache: false,
url: nanook_ajax_obj.ajaxurl,
data: {
'action':'more_post_ajax',
'cat' : cat,
'ppp' : ppp,
'page' : pageNumber
},
success: function(html, data) {
$(".products").append(html);
},
error: function(errorThrown) {
console.log(errorThrown);
}
});
});
});
来自functions.php的函数
function more_post_ajax(){
$ppp = (isset($_POST['ppp'])) ? $_POST["ppp"] : 4;
$page = (isset($_POST['page'])) ? $_POST['page'] : 2;
$cat = (isset($_POST['cat'])) ? $_POST['cat'] : '';
$args = array(
'post_type' => 'product',
'posts_per_page' => $ppp,
'cat' => $cat,
'paged' => $page
);
$loop = new WP_Query($args);
$postcount = $loop->max_num_pages;
$out = '';
?>
<ul class="products">
<?php
echo $postcount;
if ($loop -> have_posts()) : while ($loop -> have_posts()) : $loop -> the_post();
$out .= wc_get_template_part( 'content', 'product' );
endwhile;
?>
</ul>
<?php
endif;
wp_reset_postdata();
die();
}
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
继承了视觉作曲家元素代码:
// Product Grid
function productgrid_func( $atts ) {
ob_start();
extract( shortcode_atts( array(
'nanook_extra_class' => '',
), $atts ) );
?>
<div id="ajax-posts" class="row">
<?php
$postsPerPage = 8;
$cat = '';
$args = array(
'post_type' => 'product',
'posts_per_page' => $postsPerPage,
'product_cat' => $cat
);
$loop = new WP_Query($args);
$cat_id = $cat;
?>
<div class="et-product-cats">
<a class="cat-trigger active" href="">
<?php echo 'All'; ?></a>
<?php $wcatTerms =
get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0));
foreach($wcatTerms as
$wcatTerm) :
?>
<a class="cat-trigger" href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
<?php endforeach; ?>
</div>
<div class="et-product-filter-button">
<span>Filter</span>
</div>
<div class="product-grid-cover hidden">
<div class="product-grid-preloader">
</div>
</div>
<ul class="products">
<?php
while ($loop->have_posts()) : $loop->the_post();
?>
<?php $output .= wc_get_template_part( 'content', 'product' );
endwhile;
?>
</ul>
<?php
?>
</div>
<div class="et-load-more" data-category="">
<span>Load More</span>
</div>
add_shortcode( 'productgrid', 'productgrid_func' );
add_action( 'vc_before_init', 'productgrid_integrateWithVC' );
答案 0 :(得分:0)
您需要获取与之交互的该类的实例。在事件处理程序this
中将是匹配选择器
更改
var cat = $('.et-load-more').data('category');
要
var cat = $(this).data('category');
当您使用$('.et-load-more').data('category');
查找页面中的所有课程时,会发生什么。但它只能返回一个值,所以它从第一个