wordpress网站页面登录才能访问

2021-11-20 1232阅读

文章最后更新时间:2022年02月18日已超过797天没有更新。

   代码需要考虑两个东西,1、判断登录,2、跳转登录并登录跳转本页面(获取本页面链接)。这两个东西我都整合到我的一行代码了,代码如下:

<?php if(!is_user_logged_in()){header('Location: https://xiaozu.uesdto.com/wp-login.php?redirect_to='.esc_url( home_url( $_SERVER['REQUEST_URI'] )));exit;}?>

        也是够简单的,但也是经常遇到记录下,比如我新建了个页面模板,加入其中,新建页面时选择它,页面就只有登录才能看到了,代码如下:

<?php
/**
 * Template Name: 全宽登录可见模板
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage beehive
 * @since 1.0.0
 */
/** Do not allow directly accessing this file. */
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct script access denied.' ); } ?>
<?php if(!is_user_logged_in()){header('Location: https://xiaozu.uesdto.com/wp-login.php?redirect_to='.esc_url( home_url( $_SERVER['REQUEST_URI'] )));exit;}?>
<?php beehive()->layout->set( 'full' ); ?>
<?php get_header(); ?>
<?php get_template_part( 'template-parts/before-template' ); ?>
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'page' );
/** If comments are open or we have at least one comment, load up the comment template. */
if ( comments_open() || get_comments_number() ) :
comments_template();
  endif;
endwhile; // End of the loop.
?>
<?php wp_reset_postdata(); ?>
  
<?php get_template_part( 'template-parts/after-template' ); ?>
<?php
get_footer();