If Else Gravatar Author Picture

Ok so i have added a custom field inside of the WordPress edit profile field where a contributor and above can add a custom image if they do not use gravatar. Now i am trying to write a if else statement and this is what i have

<?
    $hasauthorpic = (the_author_meta('author_pic'));
    if (function_exists('get_avatar')) { echo get_avatar( get_the_author_email(), '80' );}
    else {echo '?><img class="avatar avatar-80 photo"><? $hasauthorpic ?></img><? ';}
?>

What i want to try and do is if the user does have a gravatar use it unless they have specified a link to there profile pic. Or have the author_pic become higher priority even if user has gravatar.

Read More

EDIT:

<?
$authorpic = the_author_meta('author_pic');
$gravatar = get_avatar( get_the_author_email(), '80' );
    if ($authorpic); 
    elseif (function_exists('get_avatar')) 
            echo ($gravatar); 
?>

OK so i tried the code below and that did not quite work. Maybe because i am putting this into a single-whatever.php file. The above is what i have managed to get but the only problem is when it shows it shows both the avatar and author pic link so i know i still need to add the <img> tags but that will be easy later. The one thing i did read is that your cant put a true on the_author_meta so i need help.

If you can come up with a code to hook into the gravatar stuff then ill take it. In other words if you have a code that i can place in my functions.php file thatll work to i and i would prefer that. the field name is author_pic

UPDATE: This is my final write up with the provided code from below

<?php
$authorpic = get_the_author_meta('author_pic');
$imgtagbeg = ('<img style="height:80px; width:80px" src="');
$imgtagend = ('"/>');
if ($authorpic)
    echo $imgtagbeg,$authorpic,$imgtagend; 
else
    echo get_avatar( get_the_author_email(), '80' ); 
?>

Related posts

Leave a Reply

4 comments

  1. You need to use get_the_author_meta() instead of the_author_meta()

    <?php
    $authorpic = get_the_author_meta('author_pic');
    if ($authorpic)
        echo $authorpic; 
    else
        echo get_avatar( get_the_author_email(), '80' ); 
    ?>
    
  2. Your if will always return true and bypass your custom author pic.

    Run the if on the custom field. If it returns true then add a filter to get_avatar that uses the custom field author pic.

    if ( the_author_meta('author_pic') ) {
    add_filter( 'get_avatar', 'your_custom_author_pic_function' );
    }