Modifying WP_LIST_AUTHOR Functions to output all users in a grid (and Paginate)

The title says it all.

I’ve been taking WP_LIST_AUTHOR and modifying it to create more of a member directory than just a list of authors while leaving a lot of the original options intact (and I am going to add more down the line). My first task was to build pagination into it, which I managed to do successfully (code below) [although it’s not very pretty yet].

Read More

I now want it to build this information into tables (probably 15 results per page, in a 3×5 grid. Row – cell cell cell Row – cell cell cell etc). I am not really sure on the best way to do this, or if this can even be done with the current way the code is constructed, any advice or linked resources would be greatly appreciated! I’m really just hacking things together 😉 Also note; the site uses s2member plugin and any code will need to not mess with that functionality.

<?php

///////////////////////
/////  SEXY TIME  /////
///////////////////////

/** 
 * CUSTOM FUNCTIONS BY DUIWEL
 * We are taking the regular wp_list_authors and forcing it to always display all
 * the authors, as well as have pagination and a better format
 *
 * This is my first attempt at a WordPress Hack such as this
 * 
 * 5/28/2011
 *
 * I have left most of the original function text intact, including the comments below
 *
 * I used a lot of code from Crayon Violent at PHPFREAKS
 * http://www.phpfreaks.com/tutorial/basic-pagination
 * Most of his/her comments also remain intact
 *
 */


//ORIGINAL WP COMMENTS for wp_list_authors
/**
 * List all the authors of the blog, with several options available.
 *
 * <ul>
 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
 * author's name.</li>
 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
 * installed bydefault.</li>
 * <li>show_fullname (boolean) (false): Show their full names.</li>
 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
 * <li>feed_image (string) (''): If isn't empty, use this image to link to
 * feeds.</li>
 * <li>echo (boolean) (true): Set to false to return the output, instead of
 * echoing.</li>
 * <li>style (string) ('list'): Whether to display list of authors in list form
 * or as a string.</li>
 * <li>html (bool) (true): Whether to list the items in html form or plaintext.
 * </li>
 * </ul>
 *
 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
 * @since 1.2.0
 * @param array $args The argument array.
 * @return null|string The output, if echo is set to false.
 */

 //THE FUNCTION

function duiwel_custom_list_users($args = '') {
    global $wpdb;



    // HIDE_EMPTY ORIGINALLY TRUE, now FALSE
    $defaults = array(
        'orderby' => 'name', 'order' => 'ASC', 'number' => '',
        'optioncount' => false, 'exclude_admin' => true,
        'show_fullname' => false, 'hide_empty' => false,
        'feed' => 'feed', 'feed_image' => '', 'feed_type' => 'rss2', 'echo' => true,
        'style' => 'list', 'html' => true
    );

    $args = wp_parse_args( $args, $defaults );
    extract( $args, EXTR_SKIP );

    $return = '';

    $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
    $query_args['fields'] = 'ids';
    $authors = get_users( $query_args );

    // FYI This is the post count of each author, not the total count of authors
    $author_count = array();
    foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
        $author_count[$row->post_author] = $row->count;

        // need to count 'authors' here
        $totalusers = count($authors);

        //////////////////////////////
        ////// PAGINATION ////////////
        //////////////////////////////

        $numrows = $totalusers;

        // number of rows to show per page
        $rowsperpage = 10;
        // find out total pages
        $totalpages = ceil($numrows / $rowsperpage);

        // get the current page or set a default
        if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
        // cast var as int
        $currentpage = (int) $_GET['currentpage'];
        } else {
        // default page num
        $currentpage = 1;
        } // end if


        // if current page is greater than total pages...
        if ($currentpage > $totalpages) {
        // set current page to last page
        $currentpage = $totalpages;
        } // end if
        // if current page is less than first page...
        if ($currentpage < 1) {
        // set current page to first page
        $currentpage = 1;
        } // end if

        // the offset of the list, based on current page 
        $offset = ($currentpage - 1) * $rowsperpage;



        //////////////////////////////
        ////// END PAGINATION ////////
        //////////////////////////////


        //////////////////////////////////////////
        ////// PAGINATION TO FOLLOW ARRAY  ///////
        //////////////////////////////////////////


        //I need to take the SQL LIMIT function from the pagination code I found
        //and incorporate it into the arrays I'm using, cause I'm not actually
        //querying a SQL table, I'm querying an array

        $pagination_user_table = $authors;

        $paged_authors = array_slice( $pagination_user_table , $offset , $rowsperpage );

        //////////////////////////////////////////
        ////// START NORMAL WP_LIST_AUTHOR  ////////
        //////////////////////////////////////////


    foreach ( $paged_authors as $author_id ) {
        $author = get_userdata( $author_id );

        if ( $exclude_admin && 'admin' == $author->display_name )
            continue;

        $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;

        if ( !$posts && $hide_empty )
            continue;

        $link = '';

        if ( $show_fullname && $author->first_name && $author->last_name )
            $name = "$author->first_name $author->last_name";
        else
            $name = $author->display_name;

        if ( !$html ) {
            $return .= $name . ', ';

            continue; // No need to go further to process HTML.
        }

        if ( 'list' == $style ) {
            $return .= '<li>';
        }

        //some extra Avatar stuff

        $avatar = 'wavatar';
        $link = get_avatar($author->user_email, '80', $avatar);



        $link .= '<div id=directoryinfo>' . ' <a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';

        if ( !empty( $feed_image ) || !empty( $feed ) ) {
            $link .= ' ';
            if ( empty( $feed_image ) ) {
            //Line breaking for RSS formatting (testing mostly)
                $link .= '<br>(';
            }

            $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';

            $alt = $title = '';
            if ( !empty( $feed ) ) {
                $title = ' title="' . esc_attr( $feed ) . '"';
                $alt = ' alt="' . esc_attr( $feed ) . '"';
                $name = $feed;
                $link .= $title;
            }

            $link .= '>';

            if ( !empty( $feed_image ) )
                $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
            else
                $link .= $name;

            $link .= '</a>';

            if ( empty( $feed_image ) )
                $link .= ')';
        }

        if ( $optioncount )
            $link .= ' ('. $posts . ')';

        $return .= $link;
        $return .= ( 'list' == $style ) ? '</li>' : ', ';
    }






    $return = rtrim($return, ', ');

    if ( !$echo )
        return $return;

    echo $return;

        /////////////////////////////////////////
        ////// END WP_LIST_AUTHOR NORMALCY //////
        /////////////////////////////////////////

    // little spacer
    echo "<br /><br />";
        //////////////////////////////
        ////// PAGINATION LINKS //////
        //////////////////////////////



/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1

   echo " <a href=' " , the_permalink() , " ?currentpage=1'><<</a> ";

   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page

      echo " <a href=' " , the_permalink() , " ?currentpage=$prevpage'><</a> ";



} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
        echo " <a href=' " , the_permalink() , " ?currentpage=$x'>$x</a> ";


      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 

            echo " <a href=' " , the_permalink() , " ?currentpage=$nextpage'>></a> ";

   // echo forward link for lastpage

            echo " <a href=' " , the_permalink() , " ?currentpage=$totalpages'>>></a> ";


} // end if
/****** end build pagination links ******/


        //////////////////////////////////
        ////// END PAGINATION LINKS //////
        //////////////////////////////////


}





///////////////////////////
/////  END SEXY TIME  /////
///////////////////////////



?>

            <div id="directorylist">
<ul>
<?php duiwel_custom_list_users() ?>
</ul>
        </div><!-- #directorylist -->

Related posts

Leave a Reply

1 comment

  1. Your snippet is a little too verbose to follow (and it’s late here) so this is more of an alternate take. I think that forking wp_list_author() might be overkill here. It would be more elegant to hook inside user search and accurately slice the portion of authors you need.

    Here is some example code I came up with:

    add_action('pre_user_query','offset_authors');
    
    $authors_per_page = 1;
    $current_page = absint(get_query_var('page'));
    
    function offset_authors( $query ) {
    
        global $current_page, $authors_per_page;
    
        $offset = empty($current_page) ? 0 : ($current_page - 1) * $authors_per_page;    
        $query->query_limit = "LIMIT {$offset},{$authors_per_page}";
    }
    
    wp_list_authors();
    

    Also check out paginate_links() function for building pagination.