我试图用这个钩子重定向特定的猫用户:
// Show app-data posts only to app users
function user_redirect()
{
if ( is_category( 'app-data' ) ) {
$url = site_url();
wp_redirect( $url );
exit();
}
}
add_action( 'the_post', 'user_redirect' );但是它不起作用,我也不知道为什么。如果用户浏览该类别,则重定向。如果用户正在浏览该类别或该类别的帖子,我希望重定向。
发布于 2018-06-20 09:42:45
你的代码有一个主要问题..。在任何html内容已经发送之后你不能重定向..。这样的重定向将被忽视..。
那么,为什么您的代码不正确?因为the_post钩子。当post的对象被设置时,这个钩子就会被激活。所以通常都在循环中,太晚了不能重定向.
再用一个钩子。
重定向(而且通常用于)的最佳挂钩之一是template_redirect。正如您所看到的,它是在获取标题之前启动的,所以一切都已经设置好了。
function redirect_not_app_users_if_app_data_category() {
if ( (is_category( 'app-data' ) || in_category('app-data'))&& ! is_user_logged_in() ) {
wp_redirect( home_url() );
die;
}
}
add_action( 'template_redirect', 'redirect_not_app_users_if_app_data_category');发布于 2018-06-20 09:34:21
将钩子名_post更改为template_redirect
add_action( 'template_redirect', 'wpse_restrict_catgorey');
function wpse_restrict_catgorey(){
if( ! is_user_logged_in() && is_category( 'app-data' ) ) {
wp_redirect( home_url() );
exit;
}
}https://wordpress.stackexchange.com/questions/306500
复制相似问题