仅允许管理员访问页面

时间:2012-11-17 作者:Faisal

我需要知道如何在WordPress中制作一个只能由管理员访问的页面模板。如何将其应用于以下模板?

<?php /*
Template Name: Agency Area
*/
?>
<?php get_header(); ?>

<div id="body">
    <div class="agency_area_menu">
        <?php wp_nav_menu(); ?>
    </div>
</div>

<?php get_footer(); ?>
我宁愿不用插件。

1 个回复
最合适的回答,由SO网友:Johannes Pille 整理而成
<?php
/*
Template Name: Agency Area
*/
get_header();

echo \'<div id="body">\';

    global $current_user;

    if( in_array( \'administrator\', $current_user->roles ) ) {
        echo \'<div class="agency_area_menu">\';
            wp_nav_menu();
        echo \'</div>\';
    } else {
        // echo \'<p>You do not have the rights required to view this page. Sorry.</p>\';
        /* or with internationalization
           (uncomment either, adjust text domain if applicable) */
        echo \'<p>\' .
            __(
                \'You do not have the rights required to view this page. Sorry.\',
                \'theme-text-domain\'
            ) .
            \'</p>\';
    }

echo \'</div>\';

get_footer();
?>
结束