【发布时间】:2018-02-18 04:15:36
【问题描述】:
我想在 Woocommerce 商店页面的类别列表中隐藏某个产品类别。我找到并使用了以下 sn-p 来做到这一点:
add_filter( 'get_terms', 'exclude_category', 10, 3 );
function exclude_category( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on a page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() )
{
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'books' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
我的 wp-config 中有一个调试选项设置为 true,因此当 sn-p 工作并且“书籍”类别从列表中隐藏时,我收到以下错误:
Notice:: Trying to get property of non-object in
它指向这条线:
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() )
这行写对了吗?还是我错过了什么?
【问题讨论】:
标签: php wordpress woocommerce widget custom-taxonomy