在空白的php页面中获取当前用户名(wordpress)

use*_*577 2 php wordpress username

我搜索了Stack Overflow网站以获得正确的答案(有效),但遗憾的是没有任何效果......

这是一个简单的问题,我只需要输出当前用户的用户名!

我在stackoverflow上找到了一个代码如下:

<?php
$user_id = get_current_user_id();
echo "ID User : ".$user_id ;
if ($user_id == 0) {
    echo 'You are currently not logged in.';
} else {
    echo 'You are logged in as user '.$user_id;
}
?> 
Run Code Online (Sandbox Code Playgroud)

^^在这个网站上找到它

当我这样做时,我收到一个错误:

致命错误:在第14行的/home/heal/public_html/mysite.com/index.php中调用未定义的函数get_current_user_id()

我正在使用wordpress版本3.3.1

Kam*_*shi 5

我认为您需要访问全局变量,因此您需要在新页面中加载wordpress.

if ( !defined('__DIR__') ) define('__DIR__', dirname(__FILE__)); //getting current directory using magic constants __DIR__ is deprecated in php 5.3.+
$path = explode('wp-content',__DIR__); //getting main web root path for including further files.
include_once($path[0].'wp-load.php'); //for getting global variables like wpdb 
    global $current_user;

    echo $current_user->user_login;
Run Code Online (Sandbox Code Playgroud)

添加上面后,您现在可以访问全局变量.user_login将打印用户名.我希望能帮助你.我同意@ j3frea.在这里,我向您展示了我试图实现插件的提示,所以我使用'wp-content'进行了爆炸.