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

我正在嘗試利用撰寫該帖子的作者的頭像。 我以為這樣可以完成這項工作,但似乎無法輸出正確的網址,並在圖像上顯示404。

其他人做了什么方法來瀏覽頭像圖片?

我正在尋找一個答案,該答案可以告訴我如何執行此操作,以及是否有沒有圖像無法顯示該圖像。

更新:

我試圖使用下面的代碼來完成這項工作:我應該提一下,我也在嘗試在本地計算機上進行這項工作。

echo get_avatar($authorId, 100); 

(變量使用get_the_author_id()

您需要為“ id_or_email”添加參數以獲取相應的頭像。 對於WordPress 2.7或更低版​​本,您可以使用get_the_author_id()。 對於WordPress 2.8及更高版本,您可以使用get_the_author_meta('ID')。 該函數返回字符串或假布爾值。 您可以比較返回的值以確定您是否有任何化身。 -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 ); ?> 

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

因此,在您的代碼中,嘗試獲取作者圖像:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM