Wordpress - 获取作者图片

时间:2016-11-22 21:09:15

标签: php wordpress

<?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;?>

我正试图通过撰写该帖子的作者的头像。 我认为这会使这项工作成功,但它似乎没有输出正确的网址,并在图像上给我404.

其他人通过什么方法来浏览头像?

我正在寻找一个答案,告诉我该怎么做,以及图片是否没有显示。

更新:

我尝试使用下面的代码来完成这项工作:我应该提一下,我正在努力在本地计算机上完成这项工作。

echo get_avatar($authorId, 100); 

(变量使用get_the_author_id()

2 个答案:

答案 0 :(得分:3)

您需要为&#34; id_or_email&#34;添加参数。获取合适的头像。对于WordPress 2.7或更低版​​本,您可以使用get_the_author_id()。对于 WordPress 2.8及更高版本,您可以使用get_the_author_meta(&#39; ID&#39;)。该函数返回字符串或false布尔值。您可以比较返回的值以确定您是否有任何头像。 - 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; ?>

答案 1 :(得分:0)

<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?> 

其中id_or_email是必需的。您的代码中缺少此内容。更多https://codex.wordpress.org/Function_Reference/get_avatar

因此,在您的代码中,尝试获取作者图片:

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