简体   繁体   English

Wordpress-获取作者图像

[英]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. 我以为这样可以完成这项工作,但似乎无法输出正确的网址,并在图像上显示404。

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() ) (变量使用get_the_author_id()

You'll need to add a parameter for "id_or_email" to fetch the appropriate avatar. 您需要为“ id_or_email”添加参数以获取相应的头像。 For WordPress 2.7 or lower, you can use get_the_author_id(). 对于WordPress 2.7或更低版​​本,您可以使用get_the_author_id()。 For WordPress 2.8 and up, you can use get_the_author_meta('ID'). 对于WordPress 2.8及更高版本,您可以使用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 -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. 其中id_or_email是必需的。 This is missing in your code. 您的代码中缺少此内容。 For more https://codex.wordpress.org/Function_Reference/get_avatar 有关更多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 ); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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