受密码保护的页面的自定义模板

时间:2012-11-06 作者:MultiformeIngegno

我有一个受密码保护的页面,我想为其使用自定义模板来查询特定类别的帖子。我如何才能实现query\\u帖子只有在用户提交页面密码后才能工作?我正在尝试,但不起作用(页面仅显示页眉和页脚)

<?php
/*
Template Name: xxxx
*/
?>

        <?php get_header(); ?>
            <?php if ( post_password_required() ) : ?>
            <?php query_posts(\'cat=9\'); ?>
            <?php while ( have_posts() ) : the_post(); ?>


            <a href="<?php the_permalink(); ?>"><h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1></a>
                <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
                                        <div class="entry-meta">
                    <?php edit_post_link( __( \'Edit\', \'twentyeleven\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
                    </div>


            <?php endwhile; ?>
            <?php endif; ?>


        <?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:windyjonas 整理而成

将当前页面的查询与类别中所有帖子的查询分开
未测试示例:

<?php
/*
Template Name: xxxx
*/
?>

<?php while ( have_posts() ) : the_post(); ?>
<?php get_header(); ?>
<?php if ( post_password_required() ) :
    $args = array(
        \'cat\' => 9,
        \'numberposts\' => -1,
        );
    $posts = get_posts( $args );
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <a href="<?php the_permalink(); ?>">
            <h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1>
        </a>
        <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
        <div class="entry-meta">
            <?php edit_post_link( __( \'Edit\', \'twentyeleven\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
        </div>
    <?php endforeach; ?>

<?php endif; ?>
<?php get_footer(); ?>
<?php endwhile; ?>

SO网友:Fabien Quatravaux

您的解决方案是好的multiformingegno,但您应该使用WP-template标记get_the_password_form() 而不是对密码表单进行编码,如中所示the Codex :

<?php
/*
Template Name: xxxx
*/
?>
    <?php get_header(); ?>
        <?php if ( post_password_required() ) : 

            // ask for the password
            echo get_the_password_form(); 
        else: 

            // authenticated user, display the content
        ?>
        <?php query_posts(\'cat=9\'); ?>
        <?php while ( have_posts() ) : the_post(); ?>


        <a href="<?php the_permalink(); ?>"><h1 style="padding-top:0;margin-top:-5px" class="entry-title"><?php the_title(); ?></h1></a>
            <div class="entry-content" style="padding:0"><?php the_content(); ?></div>
                                    <div class="entry-meta">
                <?php edit_post_link( __( \'Edit\', \'twentyeleven\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
                </div>


        <?php endwhile; ?>
        <?php endif; ?>


    <?php get_footer(); ?>

结束

相关推荐

How to do paging in the loop?

我用过single.php 对于此代码:<?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array