php - Missing Author Information

I am having trouble displaying the author name and avatar on my single.php page. The posts load fine and all the correct

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>

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>
Share Improve this question edited Apr 10, 2019 at 22:48 user5854648 asked Apr 10, 2019 at 22:37 user5854648user5854648 16317 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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; ?>

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745396703a4625912.html

相关推荐

  • php - Missing Author Information

    I am having trouble displaying the author name and avatar on my single.php page. The posts load fine and all the correct

    12小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信