下面的代码可以很好地用于页面,但如何在下面的代码中添加类别id以及页面。
add_action( \'template_redirect\', function() {
if ( is_user_logged_in() || ! is_page() ) return;
$restricted = array( 250, 253 ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( \'/user-registration\' ) );
exit();
}
});
最合适的回答,由SO网友:cybmeta 整理而成
在实际代码中可以看到:
if ( is_user_logged_in() || ! is_page() ) return;
此代码执行以下操作:如果用户登录或
is not in a page, 然后返回/什么也不做。您需要删除
! is_page()
:
add_action( \'template_redirect\', function() {
if ( is_user_logged_in() ) return;
$restricted = array( 250, 253 ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( \'/user-registration\' ) );
exit();
}
});
或者你可以离开
is_page()
并添加检查
is_category()
根据您的需要。