[英]Wordpress how can i display user role on custom page?
如何使用php在自定義頁面上顯示用戶角色? 只有一個代碼,但是僅在author.php頁面上有效。 如何在自定義頁面上運行此代碼?
<?php $aid = get_the_author_meta('ID');
$role = get_user_role($aid);
if ('subscriber' === $role)
{
echo "Subscriber";
}
elseif ('editor' === $role)
{
echo "Editor";
}`?>`
Function.php
function get_user_role($id) {
$user = new WP_User($id);
return array_shift($user->roles);
}
在任何自定義頁面中,您可以調用以下三行以拉起WordPress堆棧,然后使用任何WordPress功能
<?
//Imp to include
include('wp-load.php');
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
// check is user is logged - if yes then print its role
if(is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
echo "role is ".$role[0];
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.