这快把我逼疯了,我相信一旦有人指出答案,我会觉得自己很傻。
我有一个网站,大多数页面都需要登录才能查看。因此,如果用户登录,我将使用if语句查看页面内容,如果用户未登录,则查看登录表单。
如果我将其放在页面模板中,一切正常:
<?php get_header(); ?>
<?php if(is_user_logged_in()): ?>
<div id="wrapper">
<h5><?php the_field(\'headline\'); ?></h5>
<?php the_field(\'text\'); ?>
</div>
<?php else:
$args = array( \'pagename\' => \'request-access\' );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_content();
endwhile;
endif;
wp_reset_postdata();
endif; ?>
<?php get_footer(); ?>
这很好,但将if语句放在页眉中更有意义,这样我就不必将其复制到每个页面模板上。但如果我用完全相同的if语句:<?php if(is_user_logged_in()): ?>