Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solved: 8 Years, 8 Months, 2 Weeks ago Avatar Image
#1
Solved: 8 Years, 8 Months, 2 Weeks ago
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
#2
Solved: 8 Years, 8 Months, 2 Weeks ago
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.
#3
Solved: 8 Years, 8 Months, 2 Weeks ago
HI Effone,

I tried this .its not working. any suggestions

Thanks in Advance
#4
Solved: 8 Years, 8 Months, 2 Weeks ago
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.
#5
Solved: 8 Years, 8 Months, 2 Weeks ago
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.
#6
Solved: 8 Years, 8 Months, 2 Weeks ago
Hi Destroy666,

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

Thanks in advance
#7
Solved: 8 Years, 8 Months, 2 Weeks ago
^ suggested jQuery based code (post #2) also works fine. check again your code, jQuery loading & default avatar image path
#8
Solved: 8 Years, 8 Months, 2 Weeks ago
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/
#9
Solved: 8 Years, 8 Months, 2 Weeks ago
Hi Effone,

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


Forum Jump:


Users browsing this thread: 1 Guest(s)