如何禁用/隐藏 woocommerce 类别页面?

问题描述 投票:0回答:3

我使用以下代码隐藏了 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;
}

摘自:如何禁用/隐藏 woocommerce 单品页面?

php wordpress woocommerce redirect categories
3个回答
7
投票

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

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 文件或任何插件文件中。

已测试且有效

因为我认为您不想禁用产品类别功能,而只是想禁用相关的存档页面......


0
投票

如果您想完全“隐藏”页面并显示“找不到页面”(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”函数。


0
投票
add_action( 'woocommerce_product_query', 'bbloomer_hide_products_category_shop' );

function 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 );

}

请检查此示例。

© www.soinside.com 2019 - 2024. All rights reserved.