我正在尝试将某些类别排除在WooCommerce产品页面上 .
示例:如果在单个产品页面中我有"Categories: Cat1, Cat" 2“,我希望只显示Cat1 .
我尝试在单一产品模板中编辑meta.php . 我创建了一个新功能:
$categories = $product->get_category_ids();
$categoriesToRemove = array(53,76,77,78); // my ids to exclude
foreach ( $categoriesToRemove as $categoryKey => $category) {
if (($key = array_search($category, $categories)) !== false) {
unset($categories[$key]);
}
}
$categoriesNeeded = $categories;
然后我得到了WooCommerce的回音:
echo wc_get_product_category_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count($categories), 'woocommerce' ) . ' ', '</span>' );
但它仍然显示相同的类别 . 奇怪的是,当我做一个 var_dump($categories)
它显示正确的事情 .
3 回答
您应该尝试使用
get_the_terms
过滤器钩子中挂钩的自定义函数,这将排除要在单个产品页面上显示的特定产品类别:代码位于活动子主题(或活动主题)的function.php文件中 .
经过测试和工作 .
Try this:
将以下代码添加到
single-product.php
您可以通过在
get_terms
钩子上添加过滤器,然后在单个产品页面 and 上运行get_terms()
,然后从术语列表中排除上述类别ID来实现此目的,如果要获取的术语是product_cat
.您可以将此代码添加到
functions.php
.