我正在为wordpress开发一个主题,它使用woocommerce。目前,我已经开发了一个新的页面,我在这里展示产品。最初,我没有使用ajax就完成了类别的页面和过滤器,一切都很完美。但是,设计人员不喜欢在应用过滤器时重新加载页面,然后我再次使用wordpress中的ajax开发了所有内容,因为纯ajax无法工作。现在页面运行正常,第一个加载和过滤器可以使用ajax。但是问题是:当页面调用admin-ajax时,它会延迟3-5。但是,当我在本地主机上测试时,持续时间是瞬时的。
这是functions.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 .= "";
elseif($cont_post%3 == 0):
$output .= "";
$output .= "";
endif;
$output .= "";
//
$output .= "";
// $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 .= "
";
$output .= "";
$output .= "";
$output .= "";
// $output .= the_title();
$output .= $product->get_title();
$output .= "";
//the_excerpt();
$output .= "Curso: ".$product->get_attribute('Curso')." ";
$output .= "Inicio: ". $product->get_attribute('Inicio')." ";
$output .= "Finalización: ". $product->get_attribute('Finalizacion')." ";
$output .= "Precio: ".$product->get_attribute('Precio')." ";
$output .= "Avalado por: ".$product->get_attribute('Avalado')." ";
$output .= "";
$output .= "";
$output .= "";
$cont_post++;
endwhile;
wp_reset_postdata();
else:
$ouput.= "";
$ouput .= "No hay cursos";
$output.="";
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?为什么admin速度这么慢,我能做些什么来减少延迟呢?谢谢发布于 2019-04-04 15:50:45
我认为问题的一部分是\WP_Query调用--在进行快速、高效的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',
);
// Create a cache key based on the parameters and try to get a transient.
$cache_key = "educaforma_filter_results_" . implode( '_', $params );
$cached_results = get_transient( $cache_key);
if ( ! empty( $cached_results ) ) {
echo $cached_results;
exit;
}
$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 .= "";
elseif($cont_post%3 == 0):
$output .= "";
$output .= "";
endif;
$output .= "";
//
$output .= "";
// $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 .= "
";
$output .= "";
$output .= "";
$output .= "";
// $output .= the_title();
$output .= $product->get_title();
$output .= "";
//the_excerpt();
$output .= "Curso: ".$product->get_attribute('Curso')." ";
$output .= "Inicio: ". $product->get_attribute('Inicio')." ";
$output .= "Finalización: ". $product->get_attribute('Finalizacion')." ";
$output .= "Precio: ".$product->get_attribute('Precio')." ";
$output .= "Avalado por: ".$product->get_attribute('Avalado')." ";
$output .= "";
$output .= "";
$output .= "";
$cont_post++;
endwhile;
wp_reset_postdata();
else:
$ouput.= "";
$ouput .= "No hay cursos";
$output.="";
endif;
// Set the transient here!
set_transient( $cache_key, $output, DAY_IN_SECONDS );
echo $output;
}在上面的片段中,我添加了以下几行: // Create a cache key based on the parameters and try to get a transient.
$cache_key = "educaforma_filter_results_" . implode( '_', $params );
$cached_results = get_transient( $cache_key);
if ( ! empty( $cached_results ) ) {
echo $cached_results;
exit;
}这将根据参数创建一个“键”,并试图找到与该名称匹配的瞬态值。下文进一步说明: // Set the transient here!
set_transient( $cache_key, $output, DAY_IN_SECONDS );
echo $output;当我们发现它的时候,如果我们不早点停止的话,这就设置了短暂的时间。现在,下一次有人查询同一类别时,将设置临时结果,结果应该很快返回。还请注意,过期时间设置为24小时(DAY_IN_SECONDS)。您可能希望它更短或更长,并且根据更新内容的频率,您可能需要一些其他方法来使瞬变无效。查询慢度查询可能比较慢的原因之一是它使用了您的自定义参数product_cat,我假设您在某个查询的某个过滤器中解析该参数,这可能与术语关系有关。这意味着WordPress将加入多个表,这取决于您的数据库,这可能有点慢。而且,您使用setTimeout将响应延迟了600毫秒--我知道它并不大,但考虑到您在已经很慢的响应中添加了超过半秒的时间。除了缓存之外,我不知道您能做些什么来更快地提供内容,至少不需要深入了解站点的内部结构。添加某种形式的视觉反馈--比如加载旋转器--至少可以让用户知道正在发生什么事情,并且至少可以帮助您感知站点的加载时间(即使它实际上没有变得更快)。https://wordpress.stackexchange.com/questions/333486
复制相似问题