Have you ever wanted to automatically assign and display an avatar or image based on the user who submitted the post or wrote the page? In this tutorial, I’ll show you how, and it couldn’t be easier.
Let’s begin by looking at the PHP code.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $urlHome = get_option('home'); $username = get_the_author_nickname(); if ($username == "christopher") { $avatar = $urlHome . '/wp-content/themes/treble/img/robotbw.png'; } elseif ($username == "alex") { $avatar = $urlHome . '/wp-content/themes/treble/img/robotas.png'; } elseif ($username == "admin") { $avatar = $urlHome . '/wp-content/themes/treble/img/robotadmin.png'; } else { $avatar = 'false'; } ?> <?php if ($avatar != 'false') { ?> <img style="float:left; margin-right:15px; margin-bottom:5px" src="<?php echo $avatar; ?>" alt="avatar" /> <? } ?> |
And with a little bit of content and CSS, my specified users would have images, or in this case avatars, automatically displayed.
![]()
![]()
![]()
Thanks for this code, this script worked wonderfully for my site!
No problem my friend. I’m glad you gave it a try and everything worked out for you.
where do you put this code?
You can put the code anywhere you’d like the image to be displayed. You would, of course, need the webserver to support PHP.