简体   繁体   English

用户登录自定义帖子类型存档页面

[英]users login for custom post type archive page

Sorry for my English. 对不起我的英语不好。 I created custom post type in WordPress named "Gallery" and i want password protected this page with posts. 我在WordPress中创建了名为“图库”的自定义帖子类型,并且希望用帖子对此页面进行密码保护。 Not only posts. 不仅是帖子。 How i can do that? 我该怎么做? I found something like this but this is work only for single posts: 我发现了这样的内容,但这仅适用于单个帖子:

function tp_stop_guestes( $content ) {
    global $post;

    if ( $post->post_type == 'YOUR_CUSTOM_POSTTYPE' ) {
        if ( !is_user_logged_in() ) {
            $content = 'Please login to view this post';
        }
    }

    return $content;
}

add_filter( 'the_content', 'tp_stop_guestes' );

You need to redirect visitor bu using wp_redirect() function. 您需要使用wp_redirect()函数重定向访问者bu。

function admin_redirect() {
global $post;
if ( !is_user_logged_in() && $post->post_type == 'YOUR_CUSTOM_POSTTYPE' ) {
   wp_redirect( home_url('login') );
   exit;
}
}
add_action('get_header', 'admin_redirect');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM