[英]PHP to redirect 1 post is applying on all my post
I would like no redirect not logged in user who is trying to access a specific product, to My Account
page to help them register (in WooCommerce). 我想没有重定向用户 ,谁是试图访问特定的产品, 不登录
My Account
页面,以帮助他们(在WooCommerce)注册。
I'm using the code below, but at present all the products redirect to My account
: 我正在使用下面的代码,但是目前所有产品都重定向到“
My account
:
function reg_redirect(){
if( is_product(1735) && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
An idea what's wrong ? 一个主意怎么了?
That's because is_product()
doesn't accept a post ID as an argument like is_single()
and similar functions do. 这是因为
is_product()
不像is_single()
一样接受帖子ID作为参数,而类似的函数也接受。
You could simply use get_the_ID()
, though: 您可以简单地使用
get_the_ID()
:
function reg_redirect(){
if( 1735 == get_the_ID() && !is_user_logged_in() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
Note that you should also use get_permalink()
and get_option()
together to get the URL of your account page, rather than 'hard-coding' it (passing it as a string). 请注意,还应同时使用
get_permalink()
和get_option()
来获取帐户页面的URL,而不是对其进行“硬编码”(将其作为字符串传递)。
I am unfamiliar with woocommerce, but according to https://docs.woocommerce.com/wc-apidocs/function-is_product.html , I don't think is_product is the function you want. 我不熟悉woocommerce,但是根据https://docs.woocommerce.com/wc-apidocs/function-is_product.html ,我认为is_product不是您想要的功能。 Try this.
尝试这个。
global $product;
function reg_redirect(){
$id = $product->get_id();
if( $id == 1735 && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.