[英]PHP to redirect 1 post is applying on all my post
我想沒有重定向用戶 ,誰是試圖訪問特定的產品, 不登錄 My Account
頁面,以幫助他們(在WooCommerce)注冊。
我正在使用下面的代碼,但是目前所有產品都重定向到“ 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');
一個主意怎么了?
這是因為is_product()
不像is_single()
一樣接受帖子ID作為參數,而類似的函數也接受。
您可以簡單地使用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');
請注意,還應同時使用get_permalink()
和get_option()
來獲取帳戶頁面的URL,而不是對其進行“硬編碼”(將其作為字符串傳遞)。
我不熟悉woocommerce,但是根據https://docs.woocommerce.com/wc-apidocs/function-is_product.html ,我認為is_product不是您想要的功能。 嘗試這個。
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.