web-dev-qa-db-ja.com

グリッド内のすべてのユーザー(およびPaginate)を出力するようにWP_LIST_AUTHOR関数を変更する

タイトルはそれをすべて言います。

私はWP_LIST_AUTHORを取り、それを修正してオリジナルの多くのオプションをそのままにしながら、著者のリストだけではなくもっと多くのメンバーディレクトリを作成しています(そして私はもっと下に追加する予定です)。私の最初の仕事はそれにページネーションを組み入れることでした、そして、私はそれをうまくやることができました(以下のコード)(それはまだそれほどきれいではありませんが)。

私は今、それをテーブルに組み立てることを望んでいます(たぶん1ページあたり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 -->
3
Sam K

あなたのスニペットは従うには少し冗長すぎるので(そしてここで遅くなります)、これはもう一つの選択肢です。 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() 関数もチェックしてください。

1
Rarst