简体   繁体   中英

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 }

            the_content();

            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 local machine as well.

echo get_avatar($authorId, 100); 

(the variable uses get_the_author_id() )

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; ?>
<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?> 

where id_or_email is required. This is missing in your code. For more https://codex.wordpress.org/Function_Reference/get_avatar

So in your code, try to get author image:

<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM