admin管理员组

文章数量:1122832

I am trying to create a page template that restricts content to only logged-in users, if they are not logged in then they are re-directed to a specific page. I have created a page template called Restrict Access.

Here is the code that I came up with, I have tried many times to put this snippet in the page.php below and it still shows the content. Can someone please help me and put this in where it is supposed to go? Also could you look the snippet over and see if this is even good?

<?php
// Check If user is logged in - if so then show the content - if not then 
//redirect to purchase course page
if (!is_user_logged_in() ){
echo "Restricted Content!";
echo '<a href="">Go to Home Page</a>';

}else{
// Show content
the_content();
}
?>

Here is my page.php code

<?php /* Template Name: Restrict Access*/
$options = thrive_get_options_for_post(get_the_ID());

$main_content_class = ($options['sidebar_alignement'] == "right" || 
$options['sidebar_alignement'] == "left") ? $options['sidebar_alignement'] : 
"";

if ($options['sidebar_alignement'] == "right") {
$main_content_class = "left";
} elseif ($options['sidebar_alignement'] == "left") {
$main_content_class = "right";
} else {
$main_content_class = "fullWidth";
}
$sidebar_is_active = _thrive_is_active_sidebar($options);

if (!$sidebar_is_active) {
$main_content_class = "fullWidth";
}

get_header();
?>
<?php if ($options['sidebar_alignement'] == "left" && $sidebar_is_active): ?
>
<?php get_sidebar(); ?>
<?php endif; ?>
<?php if ($sidebar_is_active): ?>
<div class="bSeCont">
<?php endif; ?>
<section class="bSe <?php echo $main_content_class; ?>">

    <?php if (have_posts()): ?>

        <?php while (have_posts()): ?>

            <?php the_post(); ?>

            <?php get_template_part('content', 'single'); ?>

            <?php if (comments_open() && !post_password_required() && $options['comments_on_pages'] != 0) : ?>
                <?php comments_template('', true); ?>
            <?php elseif ((!comments_open() || post_password_required()) && get_comments_number() > 0): ?>
                <?php comments_template('/comments-disabled.php'); ?>
            <?php endif; ?>

        <?php endwhile; ?>

    <?php else: ?>

    <?php endif ?>

</section>

I am trying to create a page template that restricts content to only logged-in users, if they are not logged in then they are re-directed to a specific page. I have created a page template called Restrict Access.

Here is the code that I came up with, I have tried many times to put this snippet in the page.php below and it still shows the content. Can someone please help me and put this in where it is supposed to go? Also could you look the snippet over and see if this is even good?

<?php
// Check If user is logged in - if so then show the content - if not then 
//redirect to purchase course page
if (!is_user_logged_in() ){
echo "Restricted Content!";
echo '<a href="http://example.com">Go to Home Page</a>';

}else{
// Show content
the_content();
}
?>

Here is my page.php code

<?php /* Template Name: Restrict Access*/
$options = thrive_get_options_for_post(get_the_ID());

$main_content_class = ($options['sidebar_alignement'] == "right" || 
$options['sidebar_alignement'] == "left") ? $options['sidebar_alignement'] : 
"";

if ($options['sidebar_alignement'] == "right") {
$main_content_class = "left";
} elseif ($options['sidebar_alignement'] == "left") {
$main_content_class = "right";
} else {
$main_content_class = "fullWidth";
}
$sidebar_is_active = _thrive_is_active_sidebar($options);

if (!$sidebar_is_active) {
$main_content_class = "fullWidth";
}

get_header();
?>
<?php if ($options['sidebar_alignement'] == "left" && $sidebar_is_active): ?
>
<?php get_sidebar(); ?>
<?php endif; ?>
<?php if ($sidebar_is_active): ?>
<div class="bSeCont">
<?php endif; ?>
<section class="bSe <?php echo $main_content_class; ?>">

    <?php if (have_posts()): ?>

        <?php while (have_posts()): ?>

            <?php the_post(); ?>

            <?php get_template_part('content', 'single'); ?>

            <?php if (comments_open() && !post_password_required() && $options['comments_on_pages'] != 0) : ?>
                <?php comments_template('', true); ?>
            <?php elseif ((!comments_open() || post_password_required()) && get_comments_number() > 0): ?>
                <?php comments_template('/comments-disabled.php'); ?>
            <?php endif; ?>

        <?php endwhile; ?>

    <?php else: ?>

    <?php endif ?>

</section>
Share Improve this question asked Dec 20, 2017 at 18:38 user133608user133608 1
Add a comment  | 

1 Answer 1

Reset to default 0

This could be done in a number of ways. Please refer to the codex for further info on template hierarchy, the wordpress loop and the 'get_template_part()' function. The page.php is for dispalying every page. You should copy and rename it to page-restricted.php.

From the original page.php, remove this part - /* Template Name: Restrict Access*/. Be sure to include it in page-restricted.php. By this, you ensure that normally page contents are displayed, it's only hidden when you set the Restrict Access template in the admin when editing the page. Your code should be rewritten then thus:

<?php while (have_posts()): ?>

    <?php the_post(); ?>

    <?php if ( !is_user_logged_in() ) {

        echo "Restricted Content!";
        echo '<a href="http://example.com">Go to Home Page</a>';

    } else {

        get_template_part('content', 'single'); ?>

    } ?>

    <?php if (comments_open() && !post_password_required() && $options['comments_on_pages'] != 0) : ?>
        <?php comments_template('', true); ?>
    <?php elseif ((!comments_open() || post_password_required()) && get_comments_number() > 0): ?>
        <?php comments_template('/comments-disabled.php'); ?>
    <?php endif; ?>

<?php endwhile; ?>

本文标签: Restrict Access to LoggedIn Users Page Template