如何在WordPress内容中显示已登录的成员用户名

时间:2019-05-11 作者:Yasir Rehman

我正在处理一个网站上的页面,但此时遇到了问题,我想显示登录成员的用户名/网站内容的名字,我正在使用此代码

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->
<?php global $current_user; get_currentuserinfo(); ?>
<h5>Hi <?php echo $current_user->user_firstname; ?></h5>
<p>You have access to all of the free members stuff, enjoy :) <a href="http://example.com/wp-login.php?action=logout"> Logout </a></p>
<?php } ?>

但是,我没有显示用户名,而是显示了如下损坏的代码See Screenshot. 我正在使用WPBakery创建页面。

1 个回复
SO网友:Stephan Samuel

看起来像是$current_user->user_firstname 包含一些标记内容。

我会将该行替换为:

<h5>Hi <?php echo esc_html($current_user->user_firstname); ?></h5>

<h5>Hi <?php echo strip_tags($current_user->user_firstname); ?></h5>
其中一个(或其中一个)可能会彻底解决您的问题。