• Resolved tvorm

    (@tvorm)


    Here’s the code:

    <?php
    $display_admins = false; // not displaying GUESTS see below
    $order_by = 'display_name'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
    $role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
    $avatar_size = 128;
    $hide_empty = true; // hides authors with zero posts
    
    if(!empty($display_admins)) {
    	$blogusers = get_users('orderby='.$order_by.'&role='.$role);
    } else {
    	$admins = get_users('role=guest');
    	$exclude = array();
    	foreach($admins as $ad) {
    		$exclude[] = $ad->ID;
    	}
    	$exclude = implode(',', $exclude);
    	$blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
    }
    $authors = array();
    foreach ($blogusers as $bloguser) {
    	$user = get_userdata($bloguser->ID);
    	if(!empty($hide_empty)) {
    		$numposts = count_user_posts($user->ID);
    		if($numposts < 1) continue;
    	}
    	$authors[] = (array) $user;
    }
    
    echo '<ul>';
    foreach($authors as $author) {
    	$display_name = $author['data']->display_name;
    	$avatar = get_avatar($author['ID'], $avatar_size);
    	$description = get_userdata($author['ID'])->user_description;
    	$author_profile_url = get_author_posts_url($author['ID']);
    	$LinkedIn = get_userdata($author['ID'])->LinkedIn;
    	$googleplus = get_userdata($author['ID'])->googleplus;
    	$twitter = get_userdata($author['ID'])->twitter;
    	$position = get_userdata($author['ID'])->Position;
    
    echo '<div class="contributor">
    	<div class="contributor-avatar">
    		<a href="', $author_profile_url, '">', $avatar , '</a>
    	</div>
    	<div class="contributor-info">
    		<a href="', $author_profile_url, '"><h1>' , $display_name , '</h1> </a> <h3>'  ,$position , '</h3>' , $description , '</br> <a href="' .$author_profile_url. '">Posts by ' , $display_name , ' </a> </p>
    	</div>
    	<div class="contributor-social">
    
    //IF $LinkedIn !=""//
    	{ <a href="https://' , $LinkedIn , '"target="blank"> LinkedIn Image </a>  }
    
    	</div>
    </div>
    '; } ?>

    I’ve pieced this together from a few sources and played with it a bit, since I’m not quite able to generate stuff like this on my own yet.

    The question: under my “contributor-social” div, is it even possible to get the LinkedIn link to show only if the $LinkedIn custom field has been filled in? Thanks for any help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Before I can answer that, I urge you to only call get_userdata($author['ID']) once, assigning it to a variable. This variable will contain the entire WP_User object with all the associated data. There is no need to repeatedly query the database to get the exact same information. It will not matter on a low traffic site, but it becomes very inefficient and a problem on high volume sites. Do something like this:

    //unneeded lines commented out
    //$description = get_userdata($author['ID'])->user_description;
    //$position = get_userdata($author['ID'])->Position;
    $user = get_userdata($author['ID']);
    echo "<h3>$user->Position</h3>$user->user_description</br>";

    This also makes it easy to conditionally display data:
    if ( array_key_exists('LinkedIn', $user->data))) echo "<a href=\"https://$user->LinkedIn\" target=\"blank\"> LinkedIn Image </a>";

    Note the use of $user->data in array_key_exists() but no where else. This is because the magic methods involved do not work with array_key_exists(). Magic methods allow the incorrect reference $user->LinkedIn be mapped to the correct $user->data->LinkedIn

    Thread Starter tvorm

    (@tvorm)

    Thanks for helping me!

    This looks good; I’m now using the $user instead of all that other junk, and it works well as far as that is concerned.

    I’m not familiar with array_key_exists (got lost when you started talking about magic), so maybe I didn’t enter it correctly, but the if statement doesn’t appear to result in echoing the code I’ve got. Even if something has been entered into LinkedIn, or the others, nothing shows. Removing the if (array_key_exists… obviously results in either an unlinked image or a linked image, depending on if there was a url entered in the custom field on the author profile page.

    echo '<ul>';
    foreach($authors as $author) {
    	$user = get_userdata($author['ID']);
    	$avatar = get_avatar($author['ID'], $avatar_size);
    	$author_profile_url = get_author_posts_url($author['ID']);	
    
    echo '<div class="contributor">
    	<div class="contributor-avatar">
    		<a href="', $author_profile_url, '">', $avatar , '</a>
    		<div class="contributor-social">';
    
    if (array_key_exists('LinkedIn', $user->data)) echo
    	'<a href="https://' , $user->LinkedIn , '"target="blank"> <img src="https://mysite.com/wp-content/uploads/2013/10/linkedin.png" alt="Linked In"> </a>';
    if (array_key_exists('twitter', $user->data)) echo
    		'<a href="https://' , $user->twitter , '"target="blank"> <img src="https://mysite.com/wp-content/uploads/2013/10/twitter.png" alt="Twitter"> </a>';
    if (array_key_exists('googleplus', $user->data)) echo
    		'<a href="https://' , $user->googleplus , '"target="blank"> <img src="https://mysite.com/wp-content/uploads/2013/10/googleplus.png" alt="Google+"> </a>';
    echo
           '</div>
    	</div>
    	<div class="contributor-info">
    			<a href="' , $author_profile_url ,'"><h1>' , $user->display_name , '</h1> </a> <h3> '  , $user->Position , '</h3>' , $user->description , '</br> <a href="' .$author_profile_url. '"></br>Posts by ' , $user->display_name , ' </a> </p>  
    
    	</div>
    </div>'
    Thread Starter tvorm

    (@tvorm)

    Very interesting. I was playing around, and using

    if (!empty($user->LinkedIn)) echo

    instead seems to be having the desired effect.

    Using isset didn’t work, neither did if ($user->LinkedIn!="").

    Thank you for your help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP author list with conditional social icons’ is closed to new replies.