限制对已登录用户的访问页面模板

时间:2017-12-20 作者:Rockin_80\'s

我正在尝试创建一个页面模板,该模板将内容限制为仅登录的用户,如果他们未登录,则会将其重定向到特定页面。我创建了一个名为“限制访问”的页面模板。

这是我想到的代码,我曾多次尝试将此代码片段放到页面中。php,它仍然显示内容。有人能帮我把这个放在应该放的地方吗?另外,你能看一下这个片段,看看这是否好?

<?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();
}
?>
这是我的页面。PHP代码

<?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>

1 个回复
SO网友:galingong

这可以通过多种方式实现。有关更多信息,请参阅食品法典template hierarchy, 这个wordpress loop 以及\'get_template_part()\' 作用页面。php用于显示每个页面。您应该将其复制并重命名为page restricted。php。

从原始页面。php,删除此部分-/* Template Name: Restrict Access*/. 请确保将其包含在页面受限中。php。这样,您可以确保页面内容正常显示,只有在编辑页面时在管理中设置“限制访问”模板时,页面内容才会隐藏。您的代码应该重写,因此:

<?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; ?>

结束

相关推荐