gpt4 book ai didi

php - 在 Woocommerce 商店页面的类别列表中隐藏产品类别

转载 作者:行者123 更新时间:2023-12-05 08:54:41 34 4
gpt4 key购买 nike

我想在 Woocommerce 商店页面的类别列表中隐藏某个产品类别。我发现并使用了以下代码片段:

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,所以当代码片段工作并且“书籍”类别从列表中隐藏时,我收到以下错误:

Notice:: Trying to get property of non-object in

它指向这一行:

if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() )

这行写对了吗?还是我遗漏了什么?

最佳答案

更新:您最好使用 unset() 从数组中删除产品类别术语,这样:

add_filter( 'get_terms', 'exclude_category', 10, 3 );
function exclude_category( $terms, $taxonomies, $args ) {
$new_terms = array();
if ( is_shop() ){
foreach ( $terms as $key => $term ) {
if( is_object ( $term ) ) {
if ( 'books' == $term->slug && $term->taxonomy = 'product_cat' ) {
unset($terms[$key]);
}
}
}
}
return $terms;
}

此代码位于您的事件子主题(或主题)的 function.php 文件中。

经过测试并可以正常工作(不会引发错误)。

关于php - 在 Woocommerce 商店页面的类别列表中隐藏产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848387/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com