![](https://i-blog.csdnimg.cn/blog_migrate/af4c6875bdc3cc1950f963df6f55d445.png)
wordpress获取封面
Recently we showed you how to create a personalized experience for your users by allowing them save their favorite posts in a personalized library. You can take personalized results to another level by using their first name at places (i.e the welcome screen). Luckily, WordPress makes it really easy to get the logged-in user’s information. In this article we will show you how to retrieve information pertaining to the currently logged in user.
最近,我们向您展示了如何通过允许用户将自己喜欢的帖子保存在个性化库中来为用户创建个性化体验。 您可以通过在各个地方(例如,欢迎屏幕)使用其名字来将个性化结果提升到另一个层次。 幸运的是,WordPress使获取登录用户的信息变得非常容易。 在本文中,我们将向您展示如何检索与当前登录用户有关的信息。
We will utilize get_currentuserinfo(); function. This could be used anywhere in your theme (header, footer, sidebar, page-template etc). In order for this to work the user must be logged-in. So we will need to use the conditional statement is_user_logged_in(). Example code:
我们将利用get_currentuserinfo(); 功能。 可以在主题的任何位置(页眉,页脚,侧边栏,页面模板等)使用它。 为了使其正常工作,用户必须登录。 因此,我们将需要使用条件语句is_user_logged_in()。 示例代码:
<?php if ( is_user_logged_in() ) { ?>
<!-- text that logged in users will see -->
<?php } else { ?>
<!-- here is a paragraph that is shown to anyone not logged in -->
<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>
Now for the logged_in users, we can show a custom message for instance, “Hey Syed, Everything is here, right where you hoped it would be”. The above code will turn into something like this:
现在,对于已登录的用户,我们可以显示一条自定义消息,例如“嘿,Syed,一切都在这里,就在您希望的位置”。 上面的代码将变成这样:
<?php if ( is_user_logged_in() ) { ?>
<!-- text that logged in users will see -->
<?php global $current_user; get_currentuserinfo(); ?>
<h1>Hi <?php echo $current_user->user_firstname; ?></h1>
<p>Everything is here, right where you hoped it would be :)</p>
<?php } else { ?>
<!-- here is a paragraph that is shown to anyone not logged in -->
<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>
The magic code that we added above is $current_user->user_firstname; which is working because the call to get_currentuserinfo() places the current user’s info into $current_user. You can use the similar method to get other information about the user such as their login, user ID, email, website etc.
我们上面添加的魔术代码是$ current_user-> user_firstname; 之所以有效,是因为对get_currentuserinfo()的调用将当前用户的信息放入$ current_user 。 您可以使用类似的方法来获取有关用户的其他信息,例如他们的登录名,用户ID,电子邮件,网站等。
Here is a sample usage of all information:
这是所有信息的示例用法:
<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "
";
echo 'User email: ' . $current_user->user_email . "
";
echo 'User first name: ' . $current_user->user_firstname . "
";
echo 'User last name: ' . $current_user->user_lastname . "
";
echo 'User display name: ' . $current_user->display_name . "
";
echo 'User ID: ' . $current_user->ID . "
";
?>
Hope this helps. Combining this with the ability to add favorite posts, you can easily create a personalized experience.
希望这可以帮助。 结合添加喜欢的帖子的功能 ,您可以轻松创建个性化的体验。
wordpress获取封面