2

I use a page template to show custom post type.

All content from the custom post type should be password protected. I´d like to use the wordpress editor for some additional public information. Can I exclude the_content(); from wordpress password protection?

page-my-custom-post-type.php

// ================ PUBLIC AREA BEGINNS ================
<h4>Pubic-Area</h4>

<?php
if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; 
endif;
// ================ PUBLIC AREA ENDS ================   




// ================ PROTECTED AREA BEGINNS ================
if ( post_password_required() ) {
        echo get_the_password_form();
}
else {


$index = 'A';
$terms = get_terms('marke');

foreach ($terms as $term) {
    if($index != strtoupper(substr($term->name, 0, 1))) {
        $index = strtoupper(substr($term->name, 0, 1));

        echo '<h1>'. $index . '</h1>';
    }

    ?>
    <h2><?php echo $term->name; ?></h2>
    <?php $args = array( 'post_type' => 'cpt_auto', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 
        'tax_query' => array(
            array(
                'taxonomy' => 'marke',
                'field' => 'slug',
                'terms' => array($term->slug)
            )
        )
    );

    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();

    endwhile;

}
?>


<?php }
// ================ PROTECTED AREA ENDS ================
?>

Thanks Ogni

1

You can use the conditional tag if ( post_password_required() ) : for check, if a password required and then exclude the_content().

// view content, only if password is required
if ( post_password_required() )
    the_content();

Also it is possible that you go about the global var $post

if ( ! empty( $post->post_password ) ) :

Ot, the last chance and not so fine, bute usefull is to enhance the query:

AND post_password = '' 
4
  • I try this on my page-template.php: <?php if (have_posts()) : while (have_posts()) : the_post(); if (post_password_required() ) { the_content(); } endwhile; endif; ?> Works not for me – ogni Oct 22 '12 at 9:13
  • I just get the login form, no other content will show. – ogni Oct 22 '12 at 9:32
  • For other content use the else statement `if (post_password_required() ) { the_content(); } else { echo 'view without password'; } You can also seperate the content, see this post – bueltge Oct 23 '12 at 17:58
  • Thanks. I´d like to do: 1. Page password protected, 2. the_title and the_content should shown without enter password. 3. Custom Post Types on this page should just shown by enter the password. – ogni Oct 24 '12 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.