php - Woocommerce - 在普通页面上添加自定义类别类
问题描述
我正在尝试将自定义类添加到普通页面上的特定产品类别。
以下 php 在商店存档页面上运行良好,但我还在首页(畅销书)和其他页面上显示了产品,并且还希望添加自定义类。
我正在使用 elementor 及其“产品”小部件和 hello 主题。
function filter_woocommerce_post_class( $classes, $product ) {
// Returns true when viewing a product category archive.
if ( is_shop() ) {
// Set taxonmy
$taxonomy = 'product_cat';
// Get the terms
$terms = get_the_terms( $product->get_id(), $taxonomy );
// Error or empty
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return $classes;
}
// Loop trough
foreach ( $terms as $index => $term ) {
// Product tag Id
$term_id = $term->term_id;
// Add new class
$classes[] = 'product-in-cat-' . $term_id;
}
}
return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );
解决方案
推荐阅读
- php - Lumen 8 依赖注入 - 无法解析的依赖解析 [参数 #0 [
$app ]] 在类 Illuminate\Support\ServiceProvider - python - 如何修复 ValueError:输入形状在 python 中不与栅格重叠?
- python-3.x - 如何避免 init 函数在 Python 中返回 None
- r - 如何从R中的数据框中随机获取一些行后获取其余行
- python - 将 mmdet Mask r-cnn 结果转换为 coco 数据集以提取分割#mmdetection
- amazon-web-services - 通知在 ASG 中运行的所有 EC2 实例
- python - 重启时无法在 crontab 中启动 python 脚本
- python - 如何在子图 x 轴上对值进行排序
- ios - React Native 应用程序中的 PhaseScriptExecution 错误
- html - 在 R 中导出 html 表并识别超链接