You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中将指定产品页面重定向至首页?

解决方案:将指定页面重定向到首页

嘿,你的思路方向是对的,但现有代码里的页面判断逻辑没匹配到你要重定向的那三个页面。is_page('/product')是在判断是否访问slug为product的页面,而你需要针对individual-box、quarterly-box、semester-box这三个特定slug做判断。

直接用下面的代码替换你原来的函数就行:

function redirect_specific_box_pages_to_home() {
    // 非后台环境,且当前页面是指定的三个页面之一
    if ( !is_admin() && is_page( array( 'individual-box', 'quarterly-box', 'semester-box' ) ) ) {
        wp_redirect( home_url() );
        exit(); // 必须加上,终止后续代码执行,确保重定向生效
    }
}
add_action( 'template_redirect', 'redirect_specific_box_pages_to_home' );

补充说明:

  • is_page()支持传入slug数组,能一次性匹配多个页面,比写多个||逻辑判断更简洁高效。
  • 建议把这段代码添加到子主题的functions.php文件末尾,或者使用自定义代码管理插件(避免直接修改父主题文件,防止主题更新时代码丢失)。
  • 如果这三个页面是WooCommerce的产品(而非普通WordPress页面),需要调整判断条件:
    function redirect_specific_products_to_home() {
        if ( !is_admin() && is_singular('product') ) {
            $current_product_slug = get_post()->post_name;
            $target_slugs = array( 'individual-box', 'quarterly-box', 'semester-box' );
            if ( in_array( $current_product_slug, $target_slugs ) ) {
                wp_redirect( home_url() );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'redirect_specific_products_to_home' );
    

这样设置后,用户访问那三个页面时就会自动跳转到首页啦。

内容的提问来源于stack exchange,提问作者Felipe Evangelista

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:51:00