Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By cnario on
Hi guys!
I'm trying to create a custom welcome block in drupal 7. I've been able to print Username an all the other links that i want, but have not been able to figure out how to print the current logged-in user picture in a block.
Here's the code I've been trying to adapt to no avail:
<?php
$user = user_load(arg(1));
if($user->picture){
print theme('image_style',
array(
'style_name' => 'avatar',
'path' => $user->picture->uri,
'width' => NULL,
'height' => NULL,
'attributes' => array('class' => 'profile-avatar')
)
);
}else{
echo '<img src=" ' . base_path() . drupal_get_path('theme', 'theme_path') . '/images/user_icon.png" />' ;
}
?>
The above code only shows up when I'm viewing a user's profile (it displays the picture of the user being viewed as opposed to current logged-in user). I want to know how to show the current logged-in user picture on all pages where the block is displayed.
Comments
Change this:<?php$user = user
Change this:
To this:
I did that already but got
I did that already but got the following message:
Notice: Trying to get property of non-object in eval() (line 9 of /home/nollycom/public_html/modules/php/php.module(80) : eval()'d code)
global $user;
it displays the default
it displays the default picture instead instead of the user picture. What could be wrong with the code?
Did you ever resolve why the
Did you ever resolve why the default picture was displayed instead of the user's?
I'm running into a similar issue.
What does your code look like
What does your code look like?
Was resolved by using a user
Was resolved by using a user_load to get the full object.
Lapse of memory with regards to the global being a slimmed down version
give token a try
Using [current:user-picture] token in a block will display the currently logged in user
Token not used in blocks
By default, token may not be used
The best way to get this is to use views.
With fields being user picture, you can filter down to current logged in user.
With this method, u avoid using php and going down to template