修改WP_LIST_AUTHER函数以输出网格中的所有用户(和分页)

时间:2011-05-29 作者:Sam K

标题说明了一切。

我一直在使用WP\\u LIST\\u AUTHOR并对其进行修改,以创建更多的成员目录,而不仅仅是一个作者列表,同时保留许多原始选项不变(接下来我将添加更多)。我的第一个任务是在其中构建分页,我成功地做到了这一点(下面的代码)[虽然还不是很漂亮]。

我现在希望它将这些信息构建到表格中(可能每页15个结果,在3x5网格中。行-单元格-单元格-行-单元格-单元格等)。我真的不确定最好的方法是什么,或者如果可以用当前的代码构造方式来完成,那么任何建议或链接的资源都将不胜感激!我真的只是把事情拼凑在一起;)另请注意;该网站使用s2member插件,任何代码都不需要干扰该功能。

<?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 -->

1 个回复
最合适的回答,由SO网友:Rarst 整理而成

您的代码片段有点太冗长,无法理解(现在已经很晚了),因此这是一个替代方案。我认为分叉wp_list_author() 这可能太过分了。在用户搜索中挂钩,并准确地分割出您需要的作者部分,这将更加优雅。

下面是我想到的一些示例代码:

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();
也请查看paginate_links() 用于生成分页的函数。

结束

相关推荐

Pagination with custom loop

我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别php:<?php if ($cat) { $cols = 2; $rows = 4; $paged = ((\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page = $cols * $rows; // -1 s