首页 > 解决方案 > 完全禁用 WooCommerce 商店页面,但保留搜索页面

问题描述

我对 WooCommerce 有疑问。我想完全禁用商店页面,但我仍然希望用户能够搜索产品。

这就是我现在所拥有的——它可以工作,但它也禁用了搜索页面,因为它使用了商店页面。

function woocommerce_disable_shop_page() {
    global $post;
    if (is_shop()):
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
    endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );

希望你能帮忙。

标签: phpwordpresswoocommerce

解决方案


我刚刚修改了您问题中的代码并且它正在工作。请在主题functions.php文件或自定义插件文件中添加以下代码。

function woocommerce_disable_shop_page() {
    global $post;
    if (is_shop() && !(is_search())):
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
    endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );

推荐阅读