题
我正在与该代码在这里显示一个节点上的userpicture
<?php
$user_load = user_load($node->uid);
$imgtag = theme('imagecache', 'avatar_node', $user_load->picture, $user_load->name, $user_load->name);
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
print l($imgtag, 'u/'.$user_load->name, $attributes);
?>
除非该用户没有图片,在这种情况下,它看起来很奇怪这样的伟大工程。
如何加载默认的图片,如果用户没有图片。 我不相信我有机会获得$画面在此块。
预先感谢。
解决方案
$user_load = user_load($node->uid);
$picture = $user_load->picture ? $user_load->picture : variable_get('user_picture_default', ''); //
$imgtag = theme('imagecache', 'avatar_node', $picture, $user_load->name, $user_load->name);
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
print l($imgtag, 'u/'.$user_load->name, $attributes);
如果你默认的图片复制到文件的目录,你可以通过 HTTP确定它:// api.drupal.org/api/function/file_directory_path/6
其他提示
这是我使用的是什么了类似的情况:
if (!empty($user->picture) && file_exists($user->picture)) {
$picture = file_create_url($user->picture);
}
else if (variable_get('user_picture_default', '')) {
$picture = variable_get('user_picture_default', '');
}
if (isset($picture)) {
$alt = t("@user's picture", array('@user' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous'))));
$picture = theme('image', $picture, $alt, $alt, '', FALSE);
if (!empty($user->uid) && user_access('access user profiles')) {
$attributes = array(
'attributes' => array('title' => t('View user profile.')),
'html' => TRUE,
);
echo l($picture, "user/$user->uid", $attributes);
}
}
//: 在Drupal 7的使用:
global $user;
file_create_url( file_load($user->picture)->uri)
不隶属于 StackOverflow