1

I`m using my own page template, for display post from some category:

$wp_query = new WP_Query();
$wp_query->query('&cat=9' . '&paged=' . $paged);
    while ($wp_query->have_posts()) :
        $wp_query->the_post(); ?>
        <div class="post-list">
            <div class="post-list-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

            <div class="short-text">
                <?php the_content('More >>'); ?>
            </div>
        </div>
    <?php endwhile; ?>
<?php wp_reset_postdata(); ?>

But when i create password for this page, content of the page are shown. I need to create password protection only for this page, and when i enter pass, all post from this page are shown.

2

This is because password protection applied on get_the_content() function. And you are not using it instead you've written your own custom loop.

So you can alter the code before loop and check if page is not password protected using function post_password_required() and then display form using get_the_password_form() else display loop.

Example:-

if ( post_password_required( get_the_ID() ) ) {
    echo get_the_password_form( get_the_ID() );
} else {
    $wp_query = new WP_Query();
        $wp_query->query('&cat=9' . '&paged=' . $paged);
        while ($wp_query->have_posts()) :
            $wp_query->the_post(); ?>
            <div class="post-list">
                <div class="post-list-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

                <div class="short-text">
                    <?php the_content('More >>'); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php wp_reset_postdata();
}
-2
<?php global $post;

if ( post_password_required( $post ) ) {
    echo get_the_password_form( $post );
} else {
    $wp_query_pass = new WP_Query('&cat=9' . '&paged=' . $paged);
        while ($wp_query_pass->have_posts()) :
            $wp_query_pass->the_post(); ?>
            <div class="post-list">
                <div class="post-list-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

                <div class="short-text">
                    <?php the_content('Next >>'); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php wp_reset_postdata();
}
  • Code only answers are considered bad quality. Please, consider to explain what the code does and why it answers the question. The answer should be understood also by users with low knowledge in programming. – cybmeta May 17 '16 at 10:38

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.