性能优化:大型分类层级的幻灯片延迟加载
When you have a parent category with:
- 大量产品 (500-1000+)
- 深度类别层级 (multiple levels of subcategories)
- 动态计数启用
产品筛选器可以生成 数百个 SQL 查询 on every page load, even if the user never uses the filter. This causes:
- 页面加载缓慢(10 秒以上)
- 504 网关超时错误
- 服务器负载高
- 用户体验差
为什么会这样
启用动态计数后,WOOF 必须:
- 递归处理所有子分类
- 为每个术语执行 SQL 查询以计算产品
- 考虑当前分类上下文计算计数
示例: 带有子类别的“太阳镜”类别(男士 → 女士 → 形状 → 品牌)会产生巨大的查询负载。
传统解决方案(不足)
❌ 使用 CSS/display 规则隐藏滑动面板 – 过滤器仍在服务器上处理
❌ 禁用动态重新计数 – 失去重要功能
❌ 移除筛选选项 – 用户无法正确过滤
❌ 数据库索引 – 小改进,未能解决根本原因
最佳解决方案:延迟加载滑出式
关键概念: 仅当用户实际打开滑出窗口时才加载过滤器。
✅ 页面即时加载(无过滤器处理)
✅ 需要时过滤器可以完美工作
✅ 无服务器资源浪费
✅ Perfect for bots and casual browsers
✅ Keeps all filter functionality intact
实现
将以下代码添加到您的主题的 functions.php:
/**
* WOOF Lazy-Load Slideout Filter
* Loads filter only when user clicks to open slideout
* Solves performance issues with deep category hierarchies
*/
add_action('wp_footer', function () {
// Only on product category pages (optional - remove condition to show everywhere)
if (!is_product_category()) {
//return;
}
?>
<!-- Slideout Trigger Button -->
<div id="woof-slideout-trigger" style="position: fixed; right: 0; top: 50%; transform: translateY(-50%); z-index: 9999; cursor: pointer; background: #333; color: #fff; padding: 15px 10px; border-radius: 5px 0 0 5px; font-weight: bold; writing-mode: vertical-rl; text-orientation: mixed;">
FILTERS
</div>
<!-- Slideout Panel -->
<div id="woof-slideout-panel" style="position: fixed; right: -350px; top: 0; width: 350px; height: 100%; background: #fff; box-shadow: -2px 0 10px rgba(0,0,0,0.3); z-index: 9998; transition: right 0.3s ease; overflow-y: auto; padding: 20px;">
<!-- Close Button -->
<div style="text-align: right; margin-bottom: 15px;">
<span id="woof-slideout-close" style="cursor: pointer; font-size: 24px; font-weight: bold;">✖</span>
</div>
<!-- WOOF Filter with autohide - loads only on first click -->
<?php echo do_shortcode('[woof autohide=0 start_filtering_btn=1]'); ?>
</div>
<!-- Background Overlay -->
<div id="woof-slideout-overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 9997; display: none;"></div>
<script>
jQuery(document).ready(function ($) {
var $trigger = $('#woof-slideout-trigger');
var $panel = $('#woof-slideout-panel');
var $overlay = $('#woof-slideout-overlay');
var $close = $('#woof-slideout-close');
var filterLoaded = false;
// Open slideout
$trigger.on('click', function () {
$panel.css('right', '0');
$overlay.fadeIn(300);
// Auto-trigger WOOF "Start Filtering" button on first open
if (!filterLoaded) {
setTimeout(function () {
var $woofBtn = $('.woof_start_filtering_btn');
if ($woofBtn.length) {
$woofBtn.trigger('click');
// Hide the button after triggering
$woofBtn.hide();
filterLoaded = true;
}
}, 500);
}
});
// Close slideout
function closeSlideout() {
$panel.css('right', '-350px');
$overlay.fadeOut(300);
}
$close.on('click', closeSlideout);
$overlay.on('click', closeSlideout);
});
</script>
<?php
}, 999);
配置步骤
- 禁用您当前的滑出窗口 在 WOOF 设置中,如果启用
- 添加上面的代码 to
functions.php您当前活动主题的 - 保持这些 WOOF 设置启用:
- 缓存动态计数:是
- 缓存项:是
- 隐藏空项:是
- 动态计数:是
- 可选自定义:
- 调整侧边栏宽度:更改
width: 350px和right: -350px - 更改按钮位置:修改
top: 50% - 自定义颜色:更新
background和color值 - 使其在全站生效:删除
if (!is_product_category())条件
- 调整侧边栏宽度:更改
工作原理
- 页面加载: 仅渲染触发按钮(不处理过滤器)
- 用户点击按钮: Slideout 打开,覆盖层出现
- 仅第一次打开: JavaScript 自动点击隐藏的“开始过滤”按钮
- 过滤器加载: WOOF 处理所有查询并显示过滤器选项
- 后续打开: 过滤器已加载,即时打开
实际结果
之前:
- 页面加载:10-15 秒(或 504 超时)
- 每次页面视图 500+ SQL 查询
- 高跳出率
之后:
- 页面加载:< 2 秒
- Filter loads in ~3 seconds (only when requested)
- 服务器负载减少约 50%(许多用户从不打开过滤器)
Compatibility
✅ 适用于所有 WOOF 版本
✅ 兼容页面缓存(WP Rocket、LiteSpeed 等)
✅ 移动友好
✅ 适用于任何 WordPress 主题
高级自定义
匹配您的主题风格:
/* Add to your theme's CSS */
#woof-slideout-trigger {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: 'Your Theme Font', sans-serif;
letter-spacing: 2px;
}
#woof-slideout-panel {
background: #f8f9fa;
font-family: 'Your Theme Font', sans-serif;
}
#woof-slideout-trigger:hover {
transform: translateY(-50%) scale(1.05);
}
左侧滑出:
更改这些值:
right: -350px → left: -350px
right: 0 → left: 0
right: -350px → left: -350px
何时使用此解决方案
何时使用懒加载滑出:
- 类别有深层层级(3+级)
- 类别中 500+ 件商品
- 需要动态重新计数
- 页面加载时间 > 5 秒
- 遇到 504 超时
侧边栏过滤器的替代方案
如果您更喜欢侧边栏而不是滑出式,请使用:
echo do_shortcode('[woof autohide=1 start_filtering_btn=1]');
这会在您的侧边栏创建一个“开始过滤”按钮,该按钮在点击时加载过滤器。
此解决方案在...之间提供了最佳平衡 性能 和 功能,确保页面快速加载,同时为有需要的用户维护完整的过滤功能。