Password protection template and automatic fall in post type

admin2025-06-02  1

I have a custom template that I protect with a password. This template shows a listing of posts from a custom post type.

The password on this custom template works, but the single posts are not protected with the same password. If I have the url of single post from my custom post type, I see the content of that post.

I would like the password for the custom template to also protect these posts.

Is this possible? If not, what other way can I use?

Thanks (and sorry for my English :| )

I have a custom template that I protect with a password. This template shows a listing of posts from a custom post type.

The password on this custom template works, but the single posts are not protected with the same password. If I have the url of single post from my custom post type, I see the content of that post.

I would like the password for the custom template to also protect these posts.

Is this possible? If not, what other way can I use?

Thanks (and sorry for my English :| )

Share Improve this question edited Mar 8, 2019 at 16:47 MikeNGarrett 2,6811 gold badge20 silver badges29 bronze badges asked Mar 8, 2019 at 16:36 Antonio CarboneAntonio Carbone 216 bronze badges 1
  • Show us your code, please. It's a little bit hard to guess what solutions you're using currently. – Krzysiek Dróżdż Commented Mar 8, 2019 at 17:30
Add a comment  | 

1 Answer 1

Reset to default 0

I solve in this way. In the custom template page, that contain the loop of custom post type I add this code:

<?php
/** Template Name: Restrict Area */
get_header(); 
?>

<?php if ( ! post_password_required() ) { ?>

//content of the page with loop of custom post type 

<?php } else{ 

    echo get_the_password_form($post->ID); 

} ?>

<?php get_footer(); ?>

In single-custom-post-type.php this code:

 <?php get_header(); ?>

 <?php
 if ( !post_password_required(id_page_custom_template) ) : ?>

 //content single

<?php else: ?>

    <?php echo get_the_password_form(id_page_custom_template); ?>    

<?php endif; ?>

<?php get_footer(); ?>

Where id_page_custom_template must be replaced with the id of the page with custom template.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748828432a314061.html

最新回复(0)