this code will display the newest post from a certain category. the post will only display
excerpt, beside it is a user avatar.
note: im using plugin called local avatar
//display newest post//
<?php
global $post;
$args = array( 'numberposts' => 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ):setup_postdata($post); ?>
//gets user avatar and excerpt//
<?php echo get_avatar( get_the_author_meta( 'user_email' )); ?>
<a href="<?php the_permalink(); ?>"><?php echo get_excerpt(100); ?>... </a>
<?php endforeach; ?>
according to wordpress, if i want to display the user avatar i should include the following inside the loop <?php echo get_avatar( $id_or_email, $size = '50'); ?>
this code displays the default avatar only.
so i used this, which was taken from the default wordpress template
<?php echo get_avatar( get_the_author_meta( 'user_email' )); ?> both locally uploaded avatar and gravatar works.
i just want clarity as to why the latter works and not the one found in wordpress codex.
They are both exactly the same function except one is a guideline taken from the WordPress documentation and one is in action.
Let me explain:
$id_or_emailand$size = '50'are placeholders to show you what sort of paramaters theget_avatar()function takes. Because$id_or_emailis not by default a declared variable in WordPress it has a value ofundefined. So what you are really writing is:get_avatar(undefined, 50)get_avatar()requires either an user’s ID or email address as the first parameter for it to return their avatar. Because you supply it neither of these WordPress falls back onto the default avatar.Because
get_the_author_meta( 'user_email' )returns an email address you are successfully fulfilling the required paramaters ofget_avatar(). The second paramater$sizeis optional and defaults to 96.See http://codex.wordpress.org/Function_Reference/get_avatar#Examples for more uses.