Max Lynn Max Lynn - 13 days ago 6
PHP Question

Wordpress - Get author image

<?php while ( have_posts() ) : the_post(); ?>
<section class="panel panel-white">
<div class="row single-post-content">
<?php
if ($category_name !== 'Jobs') {
?>
<h5 class="author-post__title"><?php the_author() ?></h5>
<p><?php echo get_the_date(); ?></p>
<?php } ?>


<?php the_content(); ?>

<?php
if ($category_name !== 'Jobs') {
?>

<div class="row author-post">
<div class="small-12 medium-4 column">
<img src="<?php echo get_avatar(); ?>"
alt="" />
</div>
<div class="small-12 medium-8 column">
<h5 class="author-post__title"><?php the_author() ?></h5>
<p>
Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a
galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but
also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing.
</p>
</div>
</div>

<?php } ?>
</div>
</section>

<?php endwhile;?>


I am trying to pull through the avatar of the author that has written the post.
I thought that would make this work but it doesn't seem to output the right url and gives me a 404 on the image.

What methods have other people done to pull through the avatar image?

I'm looking for an answer that tells me how to do that and if there isn't an image to not show one.

UPDATE:

I have tried to make this work using the below code: I should mention that I am trying to make this work on my localmachine as well.

echo get_avatar($authorId, 100);
and the variable uses get_the_author_id()

Answer

You'll need to add a parameter for "id_or_email" to fetch the appropriate avatar. For WordPress 2.7 or lower, you can use get_the_author_id(). For WordPress 2.8 and up, you can use get_the_author_meta('ID'). The function returns either a string or false boolean. You can compare the returned value to determine if you have any avatar or not. - get_avatar

<?php while(have_posts()): ?>
    <?php the_post(); ?>
    <section class="panel panel-white">
        <div class="row single-post-content">
            <?php if($category_name !== 'Jobs'): ?>
                <h5 class="author-post__title">
                    <?php the_author() ?>
                </h5>
                <p><?php echo get_the_date(); ?></p>

                <?php the_content(); ?>

                <div class="row author-post">
                    <div class="small-12 medium-4 column">
                        <?php if($avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
                            <img src="<?php echo $avatar; ?>" alt="">
                        <?php else: ?>
                            <img src="/images/no-image-default.jpg">
                        <?php endif; ?>
                    </div>
                    <div class="small-12 medium-8 column">
                        <h5 class="author-post__title"><?php the_author() ?></h5>
                        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing.</p>
                    </div>
                </div>
            <?php else: ?>
                <?php the_content(); ?>
            <?php endif; ?>
        </div>
    </section>
<?php endwhile; ?>
Comments