Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamiemitchell/ecb3aab8e2e2cf9dc91a7cb57cdc1d0b to your computer and use it in GitHub Desktop.
Save jamiemitchell/ecb3aab8e2e2cf9dc91a7cb57cdc1d0b to your computer and use it in GitHub Desktop.
Get Gravatar / Avatar Outside of loop WordPress.
<?php
//* Do not copy above. Add the hook where it's going. Change the function name to reflect your situation.
add_action( 'your_hook', 'themeprefix_author_gravatar_outside_loop_single_post' );
//* Author Gravatar/Avatar outside of Loop on Single posts
function themeprefix_author_gravatar_outside_loop_single_post() {
if ( is_single() ) :
global $post;
$author_id = $post->post_author;
$author_link = esc_url( get_author_posts_url( get_the_author_meta( $author_id ) ) );
$author_avatar = get_avatar( $author_id, 200 );
printf( '<a href="%s">%s</a>', $author_link, $author_avatar );
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment