2016-11-22 41 views
0
<?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圖像。WordPress的 - 獲取作者圖片

其他人通過頭像圖像做了什麼方法?

我正在尋找一個答案,告訴我如何做到這一點,如果沒有圖像不顯示一個。

UPDATE:

我曾嘗試使用下面的代碼,使這項工作:我要指出,我試圖讓我的本地機器上的這項工作爲好。

echo get_avatar($authorId, 100); 

(變量使用get_the_author_id()

+0

我已經添加了一個更新我的問題 –

回答

1

你需要添加參數「id_or_email」,以獲取相應的化身。對於WordPress 2.7或更低版​​本,您可以使用get_the_author_id()。對於 WordPress 2.8及更高版本,您可以使用get_the_author_meta('ID')。該函數返回一個字符串或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; ?> 
+0

這似乎不適用於我:/ –

+0

什麼字符串從get_avatar()返回? – Daerik

+0

該死的我得到這個:Warning:缺少get_avatar()的參數1,在第68行調用F:\ xampp \ htdocs \ vallum \ wp-content \ themes \ vallum \ single.php並在F:\ xampp \ htdocs中定義\ vallum \ wp-includes \ pluggable.php 2266行 –

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); ?> 
+0

謝謝,但我已經試過這樣:echo get_avatar($ AUTHORID,100);和變量使用get_the_author_id() –

+0

任何想法爲什麼上述不工作?我應該提到,id返回2 –