简体   繁体   中英

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. 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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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