如何disable/hide woocommerce分类页面?

How to disable/hide woocommerce category page?

我使用以下代码隐藏了 woocommerce 上的单个产品页面,效果很好。任何试图访问单个产品页面的人都会被重定向到主页。

我现在想隐藏分类页面。我不需要这些,因为我正在使用类别简码在其他页面上显示产品。谁能帮忙提供所需的代码?

//Removes links
add_filter( 'woocommerce_product_is_visible','product_invisible');
function product_invisible(){
    return false;
}

//Remove single page
add_filter( 'woocommerce_register_post_type_product','hide_product_page',12,1);
function hide_product_page($args){
    $args["publicly_queryable"]=false;
    $args["public"]=false;
    return $args;
}

取自:How to disable/hide woocommerce single product page?

您可以尝试使用这个自定义函数,当调用产品类别存档页面时,它将重定向到商店页面:

add_action( 'template_redirect', 'wc_redirect_to_shop');
function wc_redirect_to_shop() {
    // Only on product category archive pages (redirect to shop)
    if ( is_product_category() ) {
        wp_redirect( wc_get_page_permalink( 'shop' ) );
        exit();
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件。

已测试并有效

As I don't think that you want to disable product categories functionality, but just the related archive page…

add_action( 'woocommerce_product_query', 'bbloomer_hide_products_category_shop' );

函数bbloomer_hide_products_category_shop($q){

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'chairs' ), // Category slug here
       'operator' => 'NOT IN'
);


  $q->set( 'tax_query', $tax_query );

}

请检查这个例子。

如果您想完全“隐藏”该页面并显示“找不到页面”(404 错误)页面,您可以将以下内容添加到您的“functions.php”文件中:

function display_404_page_instead_of_products_category_page() {
  if ( is_product_category() ) {
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
  }
}
add_action( 'wp', 'display_404_page_instead_of_products_category_page' );

如果您还想隐藏产品标签页,只需相应地修改条件即可:

if ( is_product_category() || is_product_tag() ) {
...
}

(您也可以隐藏 - 例如 - 如果您愿意,图像浏览器页面,在这种情况下只需使用 is_attachment() 功能。)

谢谢@LoicTheAztec 让我知道“is_product_category”函数。