Dirty Bird Design Dirty Bird Design - 4 months ago 16
PHP Question

Display all authors with custom meta data

I am trying to display all authors along with the following data: Avatar, Name, Company Name (Custom meta field), Description and a link to their author page. The output HTML would be

<img src="">
<p>Name</p>
<p>Company Name</p>
<p>Description</p>
<p><a href="">Link to author page</a></p>


So far, its close but not working as expected. Half the data is returned outside the div. Any ideas?

<?php
$authors = wp_list_authors();
if ($authors) {
foreach ($authors as $author) {
$user = get_userdata($author->user_id);
echo get_avatar( $user->ID, 46 );
echo '<div class="l-four"><p>User ID ' . $user->ID . ' '
. $user->user_firstname . ' '
. $user->user_lastname . ' '
. $user->user_url . ' '
. $user->user_description
. '</p>'; ?>
<?php the_author_meta('company', $user->ID);?>
<?php echo '</div>';?><?php
$args=array(
'author' => $user->ID,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);

}
}
?>

Answer

Try a slightly different approach to your code. This makes it a lot easier to understand the HTML that will be produced. As you can see I'm not echoing any HTML, just PHP.

<?php
$authors = wp_list_authors();
if ($authors) {
  foreach ($authors as $author) {
    $user = get_userdata($author->user_id);
    echo get_avatar( $user->ID, 46 );
    ?>
    <div class="l-four">
      <p>User ID: <?php echo $user->ID ?></p>
      <p>Firstname: <?php echo $user->user_firstname ?></p>
      <p>Lastname: <?php echo $user->user_lastname?></p>
      <p>User URL: <?php echo $user->user_url ?></p>
      <p>Description: <?php echo $user->user_description ?></p>
      <p><?php echo the_author_meta('company', $user->ID); ?></p>
    </div>
    <?php 
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
  }
}
?>