0

We know from the docs that you can get an author's meta like so:

<?php get_the_author_meta( $field, $userID ); ?> 

But I would like to have a custom "About us" page which displays all the information of users with a specific role (admins and writers in this case).

Let's say I have a function (which uses custom fields of the profile) in functions.php like so:

<?php function the_author_box($userid) { ?>
  <div class="author-header clear">
    <div class="author-avatar-wrap"><?php echo get_avatar($userid); ?></div>
      <h4 class="author-name"><?php the_author_meta('display_name',$userid); ?></h4>
    </div>
    <div class="author-description">
      <p><?php the_author_meta('description',$userid); ?></p>
    </div>
    <ul class="author-meta">
      <?php if (get_the_author_meta('url',$userid)) { ?>
        <li class="url">
          <a href="<?php the_author_meta( 'url',$userid ); ?>" title="De website van <?php the_author_meta( 'display_name',$userid ); ?>">Website</a>
        </li>
      <?php if (get_the_author_meta('twitter',$userid)) { ?>
        <li class="twitter">
          <a href="http://twitter.com/<?php the_author_meta( 'twitter',$userid); ?>" title="Volg <?php the_author_meta( 'display_name',$userid); ?> op Twitter">Twitter</a>
        </li>
      </ul>
    </div>
<?php } ?>

I am guessing I can get all the user IDs with get_users. I do not need to get all the roles at the same time, so I can first get all the admins and then all the writers.

$admins = get_users('role=administrator&fields=ID');
$writers = get_users('role=author&fields=ID');

And then call the function:

<?php 
  foreach ($admins as $user) {
    the_author_box($user);
  }
  foreach ($writers as $user) {
    the_author_box($user);
  }
?>

After testing, it seems that only the author of the page is called or the first profile in the loop. My profile ID (alphabetically the first one, and also the author of that page) is put in the function, but the others are not.

  • I only want a user to be displayed once
  • Other profiles should be returned as well, but they don't seem to be put in a loop
2
  • I'm not quite sure what you're asking - what in particular do you find "not legit or preferable"? Commented Feb 3, 2014 at 11:47
  • @TheDeadMedic See my edit. Commented Feb 3, 2014 at 14:48

1 Answer 1

0

Not exactly sure what you're aiming at with your queation, because, why shouldn't it be legit to get the data you want by looping trough the available users, to be exact in one way or another you kind of have to do it this way, get the users and then get the user meta to display - so I'd say, it's legit.

Concerning your approach there is to note that get_the_author_meta() doesn't take user objects, it expects the user ID, so you either have to make use of the fields parameter of get_users() by specifying it to return an array of IDs. Like this:

$admins = get_users('role=administrator&fields=ID');

Or you specify the fields parameter to return user objects that already include the user meta. Like this:

$admins = get_users('role=administrator&fields=all_with_meta');

If you're going to use the latter option you have to change your the_author_box() function accordingly.

3
  • Thank you, I used the first option. Strangely enough though, it does not work. The function is called, but it doesn't seem to have looped. Only the author (me) is displayed (twice, because I am an administrator and a writer?). No other profiles seem to be put in the function. Commented Feb 3, 2014 at 14:46
  • Not sure what's wrong on your side, I've given it a basic test run, works fine for me. Try debugging your result from the get_users call. BTW you're having a syntax error in your the_author_box function, because you're not closing the parentheses of your if statements. @BramVanroy Commented Feb 3, 2014 at 15:03
  • My bad, forgot to upload the changes I made to the function in functions.php, silly me. Thanks again! Commented Feb 3, 2014 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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