访问用户配置文件变量 [英] Accessing user profile variables
问题描述
使用个人资料模块,我创建了一个名为 profile_real_name 的文本字段,用户在注册时会填写该文本字段。如何在node.tpl.php中访问此变量?
Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?
我使用了 dsm($ user)
函数输出用户变量,它包含除用于 profile_real_name
I used the dsm($user)
function to output the user variables and it contained everything except the data for the profile_real_name
我还在<$ c上运行了 dsm($ vars)
$ c> phptemplate_preprocess_user_profile ,我可以看到它包含一个名为account的对象,该对象的确包含我所需的信息,但该对象在$ user变量中不可用。
I also ran dsm($vars)
on the phptemplate_preprocess_user_profile
and I could see it contained an object called account which did contain the info I needed but this object isn't available in the $user variable.
非常感谢
推荐答案
如果要在node.tpl.php中访问作者的个人资料信息,则您想使用phptemplate_preprocess_node函数而不是user_profile。节点预处理功能默认情况下没有$ account对象,因此您必须将其加载到:
If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:
这在模板的phptemplate_preprocess_node函数中.php文件:
This goes in the phptemplate_preprocess_node function in your template.php file:
if ($vars['uid']) {
$vars['account'] = user_load(array('uid' => $vars['uid']));
}
然后,您将能够在您的节点中访问作者的个人资料值。tpl .php。您具体要求的值将是:
Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:
print($account->profile_real_name);
但是,听起来您可能希望节点作者的名字显示为profile_real_name值而不是他们的名字帐户名?
However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?
如果这样,更有效的方法是覆盖theme_username函数。
If so, a MUCH more efficient way would be to override the theme_username function.
不是直接询问您的要求,因此我在这里不做介绍,但是drupal.org论坛上的这篇文章将是开始Drupal 5或6的好地方:
http://drupal.org/node/122303#comment-204277
That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6: http://drupal.org/node/122303#comment-204277
这篇关于访问用户配置文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!