将空 WooCommerce 搜索结果重定向到自定义页面

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

我正在尝试为所有返回 0 结果的产品搜索结果实现重定向到自定义 URL。使用下面的代码,我在 WordPress 中看到“

Fatal error
”消息屏幕。

这是我的代码:

add_action( 'template_redirect', 'redirect_empty_search_result', 2 );
function redirect_empty_search_result() {

    if ( isset( $_GET['s'] ) && strcasecmp( $_GET['s'] ) == 0 ) {
        
        wp_safe_redirect( '/empty-search/' );
    
    exit();
    
    }
}
php redirect woocommerce template-redirect
1个回答
0
投票

我还没有检查你的代码,但“strcasecmp”需要两个参数 例如:

add_action( 'template_redirect', 'redirect_empty_search_result', 2 );
function redirect_empty_search_result() {

    if ( isset( $_GET['s'] ) && strcasecmp( $_GET['s'], '' ) == 0 ) {
    
        wp_safe_redirect( '/empty-search/' );

    exit();

}

}

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