D.A.P. Design Team

Create a Contributors List with Avatars in WordPress


Create a Contributors List with Avatars in WordPress

In this article we will show you how you can create a contributors page which will display a list of authors with avatars or userphoto and any other information that you like. This tutorial is an intermediate level tutorial.

First thing you need to do is create a custom page using this template.

Then you will need to open functions.php file in your themes folder and add the following code:

function contributors() {
 global $wpdb;

$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");

foreach($authors as $author) {
 echo "<li>";
 echo "<a href="".get_bloginfo('url')."/?author=";
 echo $author->ID;
 echo "">";
 echo get_avatar($author->ID);
 echo "</a>";
 echo '<div>';
 echo "<a href="".get_bloginfo('url')."/?author=";
 echo $author->ID;
 echo "">";
 the_author_meta('display_name', $author->ID);
 echo "</a>";
 echo "</div>";
 echo "</li>";
 }
 }

By adding this function you are telling WordPress to create a function that will display author’s name, and author’s avatar. You can change the avatar to userphoto plugin setting by simply changing the following line:

echo get_avatar($author->ID);

and replacing it with:

echo userphoto($author->ID);

You can add more features to this function such as displaying author URL and other information from the profile by following the structure used.

#authorlist li {
 clear: left;
 float: left;
 margin: 0 0 5px 0;
 }

#authorlist img.photo {
 width: 40px;
 height: 40px;
 float: left;
 }

#authorlist div.authname {
 margin: 20px 0 0 10px;
 float: left;
 }

You would also need to add the following lines to your CSS file:

Once you are done adding the function, now you would need to call it in your page-template. Open the contributors.php file or whatever you name the file. Follow the same page template as your page.php and in the loop, just add this function instead of displaying the content:

<div id="authorlist"><ul><?php contributors(); ?></ul></div>

This will provide you with a more content-rich contributors page. This trick is excellent for Multi-Author blogs.

Here’s an example of what it can look like.

If you want to have a contributors page with information like what’s displayed in the example above, you will need to make a few changes to the original function and whaddya know… Here’s some example code.

function contributors() {
 global $wpdb;

$authors = $wpdb->get_results("SELECT ID, user_nicename from  $wpdb->users WHERE display_name <> 'admin' ORDER BY  display_name");

foreach ($authors as $author ) {

echo "<li>";
 echo "<a href="/".get_bloginfo('url')."/author/";
 the_author_meta('user_nicename', $author->ID);
 echo "/">";
 echo get_avatar($author->ID);
 echo "</a>";
 echo "<div>";
 echo "<a href="/".get_bloginfo('url')."/author/";
 the_author_meta('user_nicename', $author->ID);
 echo "/">";
 the_author_meta('display_name', $author->ID);
 echo "</a>";
 echo "<br />";
 echo "Website: <a href="/";
 the_author_meta('user_url', $author->ID);
 echo "/" target='_blank'>";
 the_author_meta('user_url', $author->ID);
 echo "</a>";
 echo "<br />";
 echo "Twitter: <a href='http://twitter.com/";
 the_author_meta('twitter', $author->ID);
 echo "' target='_blank'>";
 the_author_meta('twitter', $author->ID);
 echo "</a>";
 echo "<br />";
 echo "<a href="/".get_bloginfo('url')."/author/";
 the_author_meta('user_nicename', $author->ID);
 echo "/">Visit&nbsp;";
 the_author_meta('display_name', $author->ID);
 echo "'s Profile Page";
 echo "</a>";
 echo "</div>";
 echo "</li>";
 }
 }

This code is utilizing User Photo plugin. The twitter field is being displayed using the trick mentioned in this article How to Display Author’s Twitter and Facebook in the Profile page.

The CSS for example would look like:

#authorlist ul{
 list-style: none;
 width: 600px;
 margin: 0;
 padding: 0;
 }

 #authorlist li {
 margin: 0 0 5px 0;
 list-style: none;
 height: 90px;
 padding: 15px 0 15px 0;
 border-bottom: 1px solid #ececec;
 }

#authorlist img.photo {
 width: 80px;
 height: 80px;
 float: left;
 margin: 0 15px 0 0;
 padding: 3px;
 border: 1px solid #ececec;
 }

#authorlist div.authname {
 margin: 20px 0 0 10px;
 }

You can display more information if you like by using the advanced code as your guide.

Source of this Function

Feb/2/11 Dexter 4

While working on a client’s website, I realized that the built-in function provided in Wordpress for listing authors was not sufficient enough. It only allowed for a simple list that was confined to the sidebar. If you want to create a more content-rich and useful contributors page, then that particular function is useless. Cue the robust tutorial.


Dexter
About

Dexter Adams is the founder and creative director at D.A.P. Design LLC, and co-founder of Kubashi, a lifestyle blog. He currently functions as lead designer at NC State University Athletics Marketing. Email: [email protected]


  • Travis

    You’re going to have some problems with those double quotes within another set of quotes. Going back to the source of the first function, you’ll see that the author places a \ before the inner quotes.

    function contributors() {
    global $wpdb;

    $authors = $wpdb->get_results(“SELECT ID, user_nicename from $wpdb->users ORDER BY display_name”);

    foreach($authors as $author) {
    echo “”;
    echo “ID;
    echo “\”>”;
    echo get_avatar($author->ID);
    echo “
    “;
    echo ”;
    echo “ID;
    echo “\”>”;
    the_author_meta(‘display_name’, $author->ID);
    echo “
    “;
    echo “”;
    echo “”;
    }
    }

    Otherwise, placing the code in the functions.php as displayed will break the page.

    • http://DexterAdams.com Dexter Adams

      Thanks Travis. You’re absolutely right.. and when I posted this I actually had to make that revision in the code I implemented on another site.. Making the revision to the code above now… Thanks again! Don’t want to mislead anyone.

  • Lance

    What about if you wanted Pagination to be added ?

  • http://DexterAdams.com Dexter Adams

    Lance, here’s what you can do utilizing the jQuery pagination plugin. Let me know if I need to further elaborate.

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" dir="ltr" lang="en-US"> <head> <title>Contributors List</title> <style> /*//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////// PAGINATION STYLING ////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////*/ .paginator, .postPagination{margin:0 auto 10px;font-weight:bold;} .paginator a, .postPagination a{color:#999;font-size:10px;margin-right:1px!important;} .paginator .active, .postPagination .active{padding:3px 8px!important;background-color:#222!important;border-top:1px solid #292929!important;} .paginator .inactive, .postPagination .inactive{padding:3px 8px!important;background-color:#292929!important;border-top:1px solid #333!important;} /*//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////// END PAGINATION STYLING //////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////*/ </style> <script type='text/javascript' src='http://code.jquery.com/jquery-latest.js?ver=3.2'></script> <script type="text/javascript" > ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ////////////////////* MINIFIED JQUERY PAGINATION CODE - DO NOT EDIT *//////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// var pageElement=Array();var paginatorId='';var currentPage=1;var allItems=0;var lastPage=1;var startPage=1;var itemsPerPage=5;var firstPageSymbol='First';var previousPageSymbol='Prev';var nextPageSymbol='Next';var lastPageSymbol='Last';var separator='';var paginatorPosition='bottom';var paginatorStyle=1;var enablePageOfOption=false;var enableGoToPage=false;var textGoToPage='Go to';var enableSelectNoItems=false;var textSelectNoItems='Items Per Page';var paginatorValues=Array(5,10,15,20,25,30);var anchorLink='javascript:void(0);';var showIfSinglePage=true;eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('1c.1d.1e({1f:A(){j=M;R(1g){y\'1h\':{j.S(\'<m 7="u"></m>\');z}y\'1i\':{j.N(\'<m 7="u"></m>\');z}y\'1j\':{j.S(\'<m 7="u"></m>\');j.N(\'<m 7="u"></m>\');z}T:{j.N(\'<m 7="u"></m>\')}}O()},1k:A(){$(\'.u\').1l();j.B().P()}});A O(){9(n<1)n=5;J=j.B().U;9(J%n==0)q=V(J/n);v q=V(J/n)+1;9((F<1)||(F>q))F=1;9(!1m){9(q>1)r(F,1)}v r(F,1)}A r(a,b){9(a<0){9(a==-1)a=w-1;v a=w+1}w=a;G=(w-1)*n;9(!b){j.1n("W",A(){Q();j.B().X();j.B().Y(G,n+G).P();j.1o("W")})}v{Q();j.B().X();j.B().Y(G,n+G).P()}}A Q(){$(".u").Z("");t a=\'\';t b=\'\';t c=\'\';t d=\'\';t e=\' 8 \'+w+\' 10 \'+q+\' 8(s) \';t f=\' \'+1p+\' <K 11="r(M.C);" >\';t g=\' \'+1q+\' <K 11="n=1r(M.C);O();" >\';12(t i=0;i<D.U;i++){9(n==D[i])g+=\'<x C="\'+D[i]+\'" L="L">\'+D[i]+\'</x>\';v g+=\'<x C="\'+D[i]+\'">\'+D[i]+\'</x>\'}g+=\'</K>\';9(w==1){6=\'<a k="\'+l+\'" 7="o" h="13 8" rel="nofollow">\'+14+\'</a>\'+p;a=b=6;6=\'<a k="\'+l+\'" 7="o" h="15 8" rel="nofollow">\'+16+\'</a>\'+p;a+=6;b+=6;c+=6;d+=6}v{6=\'<a k="\'+l+\'" 7="H" I="r(1);" h="13 8" rel="nofollow">\'+14+\'</a>\'+p;a=b=6;6=\'<a k="\'+l+\'" 7="H" I="r(-1);" h="15 8" rel="nofollow">\'+16+\'</a>\'+p;a+=6;b+=6;c+=6;d+=6}12(t i=1;i<=q;i++){9(i==w){a+=\'<a k="\'+l+\'" 7="o" h="8 \'+i+\'" rel="nofollow">\'+i+\'</a>\'+p;b+=\'<a k="\'+l+\'" 7="o" h="8 \'+i+\'" rel="nofollow">\'+i+\'/\'+q+\'</a>\'+p;c+=\'<a k="\'+l+\'" 7="o" h="8 \'+i+\'" rel="nofollow">\'+i+\'</a>\'+p;f+=\'<x C="\'+i+\'" L="L">\'+i+\'</x>\'}v{6=\'<a k="\'+l+\'" 7="H" I="r(\'+i+\');" h="8 \'+i+\'" rel="nofollow">\'+i+\'</a>\'+p;a+=6;c+=6;f+=\'<x C="\'+i+\'">\'+i+\'</x>\'}}f+=\'</K>\';9(w==q){6=\'<a k="\'+l+\'" 7="o" h="17 8" rel="nofollow">\'+18+\'</a>\';a+=6;b+=6;c+=6;d+=6;6=p+\'<a k="\'+l+\'" 7="o" h="19 8" rel="nofollow">\'+1a+\'</a>\';a+=6;b+=6}v{6=\'<a k="\'+l+\'" 7="H" I="r(-2);" h="17 8" rel="nofollow">\'+18+\'</a>\';a+=6;b+=6;c+=6;d+=6;6=p+\'<a k="\'+l+\'" 7="H" I="r(\'+q+\');" h="19 8" rel="nofollow">\'+1a+\'</a>\';a+=6;b+=6}R(1s){y 1:6=a;z;y 2:6=b;z;y 3:6=c;z;y 4:6=d;z;T:6=a}9(1t)6+=\'<E 7="o" h="8 1u">\'+e+\'</E>\';9(1v)6+=\'<E 7="o" h="1b 8">\'+f+\'</E>\';9(1w)6+=\'<E 7="o" h="1b 1x. 10 1y 1z 1A">\'+g+\'</E>\';$(".u").Z(6)}',62,99,'||||||style|class|Page|if||||||||title||paginatorId|href|anchorLink|div|itemsPerPage|inactive|separator|lastPage|appendContent||var|paginator|else|currentPage|option|case|break|function|children|value|paginatorValues|span|startPage|till|active|onclick|allItems|select|selected|this|after|initPaginator|show|createPaginator|switch|before|default|length|parseInt|medium|hide|slice|html|of|onchange|for|First|firstPageSymbol|Previous|previousPageSymbol|Next|nextPageSymbol|Last|lastPageSymbol|Select|jQuery|fn|extend|pagination|paginatorPosition|top|bottom|both|depagination|remove|showIfSinglePage|fadeOut|fadeIn|textGoToPage|textSelectNoItems|Number|paginatorStyle|enablePageOfOption|Information|enableGoToPage|enableSelectNoItems|no|items|per|page'.split('|'),0,{})) ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// //////////////////* END MINIFIED JQUERY PAGINATION CODE - DO NOT EDIT *////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// </script> <script> ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////* PAGINATION PREFERENCES *//////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// $(function(){ itemsPerPage = 1; paginatorStyle = 1; paginatorPosition = 'top'; enableGoToPage = false; $("#searchresult").pagination(); }); ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////* END PAGINATION PREFERENCES *////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// </script> </head> <body> <ul id="searchresult"><!-- start container for pagination --> <!-- start list of contributors to be paginated --> <?php contributors ?> <!-- end list of contributors div --> </ul> </body> </html>