2

I am having trouble displaying the author name and avatar on my single.php page. The posts load fine and all the correct information is available. However the avatar and name is no where to be found. I'm not sure what I'm doing wrong. Help is appreciated

<div class="row">
    <div>
        <h1 class="primary-color"> <?php the_title(); ?></h1>
        <span class="avatar">
            <?php echo get_avatar( $id_or_email, 30, $default, $alt, $args ); ?>
        </span>
        by <span class="primary-color"><?php echo get_the_author_meta('display_name', $author_id); ?></span> <span class="pipe">|</span> <span class="date"><?php echo get_the_date(); ?></span>
    </div>
</div><!-- END ROW -->

<div class="row">
    <?php
        while ( have_posts() ) : the_post();
    ?>

    <?php the_content(); ?>

    <?php endwhile; ?>
</div>
5

To display the author's avatar within The Loop use get_avatar() like that:

<?php print get_avatar(get_the_author_meta('ID'), '30', '', '', ['class' => 'foo-bar']); ?>

To display the author's display name within The Loop use the_author():

<?php the_author(); ?>

So put everything inside The Loop and then:

<?php
while (have_posts()) : the_post();
  ?>

  <div class="row">
    <div>
      <h1 class="primary-color">
        <?php the_title(); ?>
      </h1>
      <span class="avatar">
        <?php print get_avatar(get_the_author_meta('ID'), '30', '', '', ['class' => 'foo-bar']); ?>
      </span>
      by <span class="primary-color"><?php the_author(); ?></span>
      <span class="pipe">|</span>
      <span class="date"><?php echo get_the_date(); ?></span>
    </div>
  </div>

  <div class="row">
    <?php the_content(); ?>
  </div>

<?php endwhile; ?>
  • Thanks for this and the references, unfortunately these calls are not working. The code in my question is the whole code (minus get_header and get_footer). So I'm not sure if I am missing something else or not. – user5854648 Apr 11 at 2:28
  • @user5854648 it's because you are not using it inside the loop – Vishwa Apr 11 at 4:29
  • @user5854648 – Answer updated. – leymannx Apr 11 at 4:48
  • @leymannx Thanks so much! So, out of curiosity, why is it the title would print without it being in the loop but the author info would not? I didn't realize that was the issue for that very reason otherwise I may have caught it. – user5854648 Apr 12 at 2:28
  • @user5854648 – the_title calls get_the_title calls get_post which falls back to the global $post if omitted. The other functions don't seem to have that fallback built in. – leymannx Apr 12 at 5:11
0

Seems like you are using author info outside the loop. If you want to get that information outside the loop, you can do it as follows, in your single.php (or if you're using page template use inside it)

<?php
global $post;
$author_id = $post->post_author;
?>

<span class="avatar">
  <?php echo get_avatar($author_id, 32); ?>
</span>
by
<span class="primary-color">
  <?php the_author_meta('user_nicename', $author_id); ?>
</span>
<span class="pipe">|</span>
<span class="date"><?php echo get_the_date(); ?></span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.