我有一个woocommerce网站,当用户没有登录时,我想隐藏商店。我把这段代码放在文件中! archive-product.php,它位于我的模板'twentytwelve-child'的woocommerce文件夹中。
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
auth_redirect();
get_header( 'shop' ); ?>
Normaly'auth_redirect()'必须在登录页面中重定向我,但它不起作用。
我也尝试使用此代码,但它也不起作用。
$login = is_user_logged_in();
if ($login == FALSE ) {
wp_redirect( home_url() );
exit;
}
我做错了吗?
答案 0 :(得分:1)
谢谢你。我还添加了一些其他有用的功能。
// Redirect none registered users to a login page
function custom_redirect() {
if( (is_shop() || is_product() || is_product_category() ) && ! is_user_logged_in() ) {
wp_redirect( site_url( '/mon-compte' ) );
exit();
}
}
add_action("template_redirect","custom_redirect");
答案 1 :(得分:0)
您无需修改Woocommerce模板文件以实现您的目标。只需将以下代码添加到functions.php
即可function custom_redirect() {
if( is_shop() && ! is_user_logged_in() ) {
wp_redirect( home_url() );
exit();
}
}
add_action("template_redirect","custom_redirect");