Forums

[resolved] How do I list authors and together with author meta information in a list? (6 posts)

  1. designinfantry
    Member
    Posted 2 years ago #

    How do I list all authors of my site in a format as such?

    Alex Kwa Profile | Twitter | Email
    Rafdi Profile | Twitter | Email
    Abel Profile | Twitter | Email

    See http://www.designinfantry.com for my site. Am referring to the right sidebar.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    <?php
    //displays all users ($users will also contain all usermeta fields for each user
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
    //  echo "<pre>"; print_r($user); echo "</pre>";
        echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      }
    }
    ?>

    Take the two slashes off the // echo "<pre>"; print_r($user); echo "</pre>"; statement to see all the values available to you

  3. designinfantry
    Member
    Posted 2 years ago #

    Hello,
    thanks for your reply.

    But I would like the link of the fullname of the authors to take me to a page where all the posts are that of the authors. Exactly like how the links of wp_list_authors work.

    Can that be done?

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    Didn't test this, but after

    $user = get_userdata($bloguser->user_id);

    add

    $post_count = get_usernumposts($user->ID);
        if ($post_count) {
          $author_posts_url = get_author_posts_url($key);
          echo '<li><a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a> (' . $post_count . ') </li>';
        }

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  5. designinfantry
    Member
    Posted 2 years ago #

    Thanks

    This is my final code in the end.

    <?php
    //displays all users ($users will also contain all usermeta fields for each user
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
    $post_count = get_usernumposts($user->ID);
        if ($post_count) {
    echo '<a href="http://www.designinfantry.com/?author=' . $user->ID . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a><br />';
    	} else {
    
    		echo '&nbsp;' . $user->user_firstname . ' ' . $user->user_lastname . '<br />';
    
    		}
    
      }
    }
    ?>
  6. spcaer
    Member
    Posted 1 year ago #

    This is exactly what I've been looking for. I have 2 questions though:

    1. How can I exclude admin users or users by id?
    2. How can I sort the results by user meta field?

    In the code below I'm trying to sort the results by the State field in DESC order and also exclude the administrator role from the results.

    $blogusers = get_users_of_blog();
    		if ($blogusers) {
    		  	foreach ($blogusers as $bloguser) {
    			$user = get_userdata($bloguser->user_id);
    
    			// Get User Fields
    			$city = $user->city; $state = $user->state; $phone = $user->phone; $user_email = $user->user_email;
    			echo '<li>' . $user->user_firstname . ' ' . $user->user_lastname . '</li>';
    			if($city != null) echo('<li>' . $city . ', '  . $state . '</li>');
    			if($phone != null) echo('<li>Phone: ' . $phone . '</li>');
    			if($user_email != null) echo('<li>Email: <a href="mailto:' . $user_email . '?subject=Question from Example.com">' . $user_email . '</a></li>');
    			}
    		}

Topic Closed

This topic has been closed to new replies.

About this Topic