I’ve been creating WordPress themes and websites for many years now and I still love learning new things – like shortcodes. I knew there was a lot of power just waiting to be welded with shortcodes, but because Genesis is such a powerful and feature-rich framework, I haven’t needed to create any shortcodes of my own. A recent job though provided me the opportunity to create my own shortcode. The client wanted to show the author’s avatar in post listings and details. This particular custom child theme created by someone else didn’t have the author’s avatar implemented in the templates. So I had to figure out how to show the author’s avatar. First I thought I’d be able to use Genesis’ Simple Edits to add the code into the Post Info section. Simple Edits doesn’t accept code but it does accept shortcodes. So I had to find the code for getting the author’s avatar. I found the code here:
So, how would you go about setting up Gravatars to display with each individual post? Over at ThemeShaper, Ian Stewart recently shared an easy way to do this. You just need the following code:
<?php echo get_avatar( get_the_author_email(), '80' ); ?>
When used, WordPress will match up the e-mail address associated with the post author to determine what Gravatar to use. The 64 is the size (pixels) of the Gravatar.
Now armed with the code, I went to the WordPress’ Codex pages to find how to create a shortcode: http://codex.wordpress.org/Shortcode_API
I went in the theme’s functions file (functions.php) and added the following code for creating my first shortcode to display an author’s avatar:
//[show_author_avatar] function author_thumbnail( $atts ){ echo get_avatar( get_the_author_email(), '40' ); } add_shortcode( 'show_author_avatar', 'author_thumbnail' );
Now I needed to use the shortcode. I went into the Genesis Simple Edits section and used this for the Post Info section:
[show_author_avatar] by
<br /> After a little styling magic, this is how the post listing turned out.
I can’t wait to do another one.
Related posts:
While attempting to upload some videos this week, ...
Images always get blurred or pixelated when they a...
The Advanced Custom Fields plugin is a very useful...
I found a great resource to move the page title or...