MyBB Community Forums

Full Version: Avatar Image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi ,

iam trying to display avatar image in header part. using below code

{$mybb->user['avatar']}

suppose user have no avatar image.then how to display No image picture. how can i do this in header part. pls help me..

Thank You
A simple jQuery can do this.

In header_welcomeblock_member template add an image tag where you wanna show the avatar.
<img id="avatar"/>

Upload the default avatar image, say it is:
{$theme['imgdir']}/defaultavatar.png

Now add this script at the end of header_welcomeblock_member template:
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function(){
var user_avatar = "{$mybb->user['avatar']}";
var default_avatar = "{$theme['imgdir']}/defaultavatar.png";
jQuery( "#avatar" ).error(function() {
  jQuery(this).attr( "src", default_avatar );
}).attr( "src", user_avatar );
});
</script>

Remember to include jQuery library in headerinclude template if it is not already ...

Edit: Tweaked the code a little, checked and working as expected.
HI Effone,

I tried this .its not working. any suggestions

Thanks in Advance
I've checked it in localhost as well as server and it is working fine.
I can't shoot the trouble by 'not working' only. Still the problem that can happen is make sure the path you are using for default avatar is correct.
I suggest not to use jQuery for such changes. It's only useful if you're going to release a theme and don't want/are not allowed to include a plugin with it. Otherwise PHP is faster, especially on old devices.

My suggestion:
1. Download Template Conditionals: http://mybbhacks.zingaburga.com/showthre...464&page=5 then upload .php and .txt file to inc/plugins and activate it.
2. Instead of {$mybb->user['avatar']}, use:
<if $mybb->user['avatar'] then>{$mybb->user['avatar']}<else>images/default_avatar.gif</if>
as <img>'s src.
Hi Destroy666,

yes iam planing to release theme. but above jquery script also not working. any suggestions?

Thanks in advance
^ suggested jQuery based code (post #2) also works fine. check again your code, jQuery loading & default avatar image path
Thanks .m. for crosschecking and confirmation. Big Grin
@mp3king: I made a fiddle for you. Change the 'user_avatar' link to some unavailable and see how the alternate default avatar appears ...

http://jsfiddle.net/effone/TeVpk/1/
Hi Effone,

Thanks for fiddle. Its working . thanks a lot..
Thanks to all