php WooCommerce:过滤产品类别列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WooCommerce:过滤产品类别列表相关的知识,希望对你有一定的参考价值。
<?php
echo '<ul class="product-categories">';
//wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) );
$cat_template = '<li class="cat-item cat-item-%d"><a href="/%s/">%s</a> <span class="count"><span class="post_count"> %d </span></span></li>';
global $wpdb;
$sid = $_GET["number"];
$url = "https://mpm.server.com/api/getValue/$sid";
$cid = $this->chReq($url);
$sql = "SELECT name,term_id,slug,count(*) as post_count FROM (
SELECT DISTINCT p.post_name,t.term_id,t.name,t.slug
FROM `wp_posts` p
INNER JOIN `wp_postmeta` m ON p.ID = m.post_id
INNER JOIN `wp_term_relationships` r ON p.ID = r.object_id
INNER JOIN `wp_term_taxonomy` tt ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN `wp_terms` t ON t.term_id = tt.term_id
INNER JOIN `wp_termmeta` tm ON tm.term_id = t.term_id
WHERE p.post_type = 'product'
AND m.meta_key = '_product_attributes'
AND tt.taxonomy = 'product_cat'
AND m.meta_value LIKE '%_cid_$cid_%'
ORDER BY p.post_name
) as m
GROUP BY name,term_id,slug";
$cats = $wpdb->get_results($sql);
$all_products_count = 0;
foreach ($cats as $c) $all_products_count += (int)$c->post_count;
echo sprintf(
$cat_template,
0,
"shop",
"All Products",
$all_products_count
);
foreach ($cats as $c) {
echo sprintf(
$cat_template,
$c->term_id,
"product-category/".strtolower($c->slug),
$c->name,
$c->post_count
);
}
echo '</ul>';
?>
以上是关于php WooCommerce:过滤产品类别列表的主要内容,如果未能解决你的问题,请参考以下文章
php Woocommerce的产品类别列表
php 存档中的WooCommerce产品类别下拉列表
从特定类别获取 WooCommerce 产品
自定义帖子类型的 WooCommerce 产品
PHP中的问题过滤woocommerce产品
类别页面上产品的 pre_get_posts - woocommerce