WordPress.org

Forums

User Photo
All authors images (3 posts)

  1. cjellison
    Member
    Posted 4 years ago #

    Is it possible to display the userphotos of all of the authors on an author page? Maybe leave out the admins (by user ID, possibly).

  2. acafourek
    Member
    Posted 4 years ago #

    I've achieved this on BecomeANewYorker.com

    Unfortunately there really isnt a good way to get this done in PHP, so you will need to make a database query in order to get the data you need. This is how I did it:

    <ul class="people">
    	<?php
    	$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts .' WHERE post_status = "publish"');
    	if($authors):
    	foreach($authors as $author) {
          ?>

    This section looks in the WordPress posts database and gets you all of the post authors who have at least one published post. The 'Distinct' part of the query means that it will remove duplicates, so you end up with a list of unique published authors IDs.

    <li class="<?php the_author_meta('ID', $author->post_author); ?>">
    		<div class="avatar">
    			<?php
    		         	$user = get_the_author_meta('ID', $author->post_author);
    					if(userphoto_exists($user))
    					    userphoto($user);
    			?>
    		</div>

    This section simply pulls in the userphoto for each author (checking to see if a photo exists is covered in the User Photo plugin documentation).

    <h2>
    	<a href="<?php bloginfo('url');?>/author/<?php the_author_meta('user_nicename', $author->post_author); ?>" title="Become A New Yorker posts by <?php the_author_meta('first_name', $author->post_author); ?> <?php the_author_meta('last_name', $author->post_author); ?>">
    	<?php the_author_meta('first_name', $author->post_author); ?> <?php the_author_meta('last_name', $author->post_author); ?>
    	</a>
    </h2>
    </li>
    <?php } ?>
    <?php endif; ?>
    </ul> <!-- end people -->

    This last section puts the author first and last names under the userphoto along with link title, etc. It is a bunch of code, but is actually really simple stuff that would be about 2 lines in plain HTML. There are ways to optimize this so you arent looking up the author data repeatedly, but I just havent had a chance to go back and rework it yet. Hopefully this helps. So all the code is in one place, this is exactly what is above in one chunk:

    <ul class="people">
    	<?php
    		$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts .' WHERE post_status = "publish"');
    	        	if($authors):
    			foreach($authors as $author) {
    	?>
    
    	<li class="<?php the_author_meta('ID', $author->post_author); ?>">
    		<div class="avatar">
    			<?php
    				$user = get_the_author_meta('ID', $author->post_author);
    					if(userphoto_exists($user))
    				    userphoto($user);
    	        	?>
    	</div>
    
    <h2>
      <a href="<?php bloginfo('url');?>/author/<?php the_author_meta('user_nicename', $author->post_author); ?>" title="Become A New Yorker posts by <?php the_author_meta('first_name', $author->post_author); ?> <?php the_author_meta('last_name', $author->post_author); ?>">
    <?php the_author_meta('first_name', $author->post_author); ?> <?php the_author_meta('last_name', $author->post_author); ?>
    </a>
    </h2>
    </li>
    <?php } ?>
    <?php endif; ?>
    </ul> <!-- end people -->
  3. cjellison
    Member
    Posted 4 years ago #

    That is very, very helpful. Thank you for saving me so much time, and not to mention coffee.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • User Photo
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags