我正在为WoodPress开发一个使用woocommerce的主题。目前,我开发了一个新页面,展示产品。最初,我没有使用ajax就完成了页面和类别过滤器,一切都很好。然而,设计师不喜欢在应用过滤器时重新加载页面,然后我使用wordpress的ajax再次开发了所有内容,因为纯ajax不起作用。现在页面运行正常,第一次加载和过滤器使用ajax。但问题如下:当页面调用admin ajax时,会延迟3-5秒。然而,当我在localhost中测试时,持续时间是瞬时的。
这是函数中的函数。php:
add_action(\'wp_ajax_nopriv_filter_category_action\',\'educaforma_filter_ajax\');
add_action(\'wp_ajax_filter_category_action\',\'educaforma_filter_ajax\');
function educaforma_filter_ajax(){
$category_filter = "";
if(isset($_POST[\'category\'])){
if($_POST[\'category\'] == "todos"){
$category_filter = "";
}
else{
$category_filter = $_POST[\'category\'];
}
}
$output = "";
$params = array(
\'product_cat\' => $category_filter,
\'posts_per_page\' => 15,
\'post_type\' => \'product\',
);
$wc_query = new WP_Query($params);
$count = $wc_query->post_count;
$cont_post = 0;
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
global $product;
if($cont_post == 0):
$output .= "<div class = \'row\'>";
elseif($cont_post%3 == 0):
$output .= "</div>";
$output .= "<div class = \'row\'>";
endif;
$output .= "<div class =\'col-md-4 col-sm-4\'>";
// <!-- <div class="container-course"> -->
$output .= "<a class=\'list-course\' href=\'".$product->get_permalink()."\'>";
// $output .= the_post_thumbnail();
// $output .= $product->get_image(artiest,1,1);
$attachment_id = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_id, \'full\' );
$output .= "<div class=\'image-product\' style=\'position: relative;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
background-clip: border-box;
box-sizing: border-box;
overflow: hidden;
height: 180px;
width: 100%;
background-image: url(".$attachment[0].") !important;
\'>
<div class=\'darken-image\'></div>
</div>";
$output .= "<div class=\'separator\'></div>";
$output .= "<div class=\'container-course\'>";
$output .= "<h3 class=\'title-course\'>";
// $output .= the_title();
$output .= $product->get_title();
$output .= "</h3>";
//the_excerpt();
$output .= "<p class=\'title-attribute\'>Curso: <span class=\'value-attribute\'>".$product->get_attribute(\'Curso\')." </span></p>";
$output .= "<p class=\'title-attribute\'>Inicio: <span class=\'value-attribute\'>". $product->get_attribute(\'Inicio\')." </span></p>";
$output .= "<p class=\'title-attribute\'>Finalización: <span class=\'value-attribute\'>". $product->get_attribute(\'Finalizacion\')." </span></p>";
$output .= "<p class=\'title-attribute\'>Precio: <span class=\'value-attribute\'>".$product->get_attribute(\'Precio\')." </span></p>";
$output .= "<p class=\'title-attribute\'>Avalado por: <span class=\'value-attribute\'> ".$product->get_attribute(\'Avalado\')." </span></p>";
$output .= "</div>";
$output .= "</a>";
$output .= "</div>";
$cont_post++;
endwhile;
wp_reset_postdata();
else:
$ouput.= "<li>";
$ouput .= "No hay cursos";
$output.="</li>";
endif;
echo $output;
}
这是javascript ajax的一部分:function($){
$(document).on(\'click\',\'#negocios\',function(e){
e.preventDefault();
$.ajax({
url: filter_categories_vars.ajaxurl,
type: \'POST\',
data: {
action: \'filter_category_action\', // The name of the WP action
category: \'negocios-internacionales\',
},
// dataType: \'json\',
complete : function ( ) { // optionally to develop in any case: success or error
},
success: function ( response ) { // to develop in case of success
$(\'#cursos_catalogo\').fadeOut(600)
setTimeout(() => {
$(\'#cursos_catalogo\').html(response.substring(0,(response.length - 1)));
$(\'#cursos_catalogo\').fadeIn(600)
}, 600);
document.getElementById("negocios").classList.add("filtered");
document.getElementById("todos").classList.remove("filtered");
document.getElementById("turismo").classList.remove("filtered");
document.getElementById("habilidades").classList.remove("filtered");
},
error: function ( errorThrown ) { // to develop in case of error
console.log("ERROR")
console.log( errorThrown );
},
});
});
我想知道两件事:为什么我不能使用普通的ajax?
为什么管理ajax如此之慢,我能做些什么来减少这种延迟?
谢谢