我正尝试通过custom posts
通过ajax显示taxonomy id
,但是在进行ajax调用时,会显示重复的帖子。
以下代码脚本位于自定义主题模板中。
jQuery(document).ready(function(){
jQuery('.cate_link').click(function(){
var getLinkCat = jQuery(this).attr('cat_permalink');
var splitLink = getLinkCat.split('?');
var catName = splitLink[1];
var termId = jQuery(this).attr('term_id');
var data = {
action: 'LoadProductBasedOnTaxonomy',
catName:catName,
termId:termId
};
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
jQuery.ajax({
method: 'post',
url:ajaxurl,
dataType:'html',
data: data,
beforeSend: function (response) {
jQuery("#loading").show();
},
complete: function (response) {
jQuery("#loading").hide();
},
success: function (response) {
// alert(response)
console.log(response)
if( response.error != 'undefined' && response.error ){
return true;
} else {
setTimeout(function() {
jQuery(".row").html(response)
jQuery('#successMessage').show('1000');
}, 3000);
}
},
});
return false;
});
});
</script>
自定义主题的functions.php
文件中的以下脚本
function LoadProductBasedOnTaxonomy(){
if(isset($_POST['catName']) && !empty($_POST['catName'])){
$getCatName = explode("=", $_POST['catName']);
$catName = $getCatName[1];
}
if(isset($_POST['termId']) && !empty($_POST['termId'])){
$termId = $_POST['termId'];
}
$args = array('post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_category',
'field' => 'id',
'terms' => $termId,
)
)
);
//$getProductsPost = get_posts($args);
$getProductsPost = new WP_Query( $args );
global $post;
$html .='';
if ( $getProductsPost->have_posts() ) {
while ( $getProductsPost->have_posts() ) : $getProductsPost->the_post();
//foreach ($getProductsPost as $key => $value) {
/*$image = wp_get_attachment_image_src( get_post_thumbnail_id( $value->ID ), 'thumbnail' );
$html .='<div class="col-lg-12 content"><div class="movie_img">';
$html .='<a class="thumbnail" href="#" data-image-id="1" data-toggle="modal" data-title="" data-image="" data-target="#image-gallery">';
$html .='<img with="200px" class="img-thumbnail" src="'.$image[0].'">';
$html .='</a></div></div>';*/
$html .='<h1>'.$post->ID.'</h1>';
echo $html;
//}
endwhile;
}
wp_reset_postdata();
wp_die();
}
add_action('wp_ajax_LoadProductBasedOnTaxonomy' ,'LoadProductBasedOnTaxonomy');
add_action('wp_ajax_nopriv_LoadProductBasedOnTaxonomy','LoadProductBasedOnTaxonomy');
所以问题是我们添加了以下类别,并在类别中添加了帖子。
每日销售:3 您可能喜欢:2 最新:5
但是在进行ajax调用时,单击Daily Sale
会显示帖子4,而不是3;单击Latest
会显示帖子超过10,而不是5。
我什至不知道为什么会发生这种情况,尽管当我打印帖子总数时,它显示的是当前结果,但是在ajax调用过程中出现了问题。