列出带有链接和图片的网站的作者

时间:2011-02-12 作者:Norcross

我想做一些我认为很容易的事。我想错了。我想做的是在侧边栏上列出一个站点的所有作者,并链接到他们各自的作者页面、撰写的帖子数量和gravatar。wp\\u list\\u作者根本没有给我一个包含gravatar的选项。这是我的

        $blogusers = get_users_of_blog();
        if ($blogusers) {
          foreach ($blogusers as $bloguser) {
            $user = get_userdata($bloguser->user_id);
        $post_count = count_user_posts($user->ID);
            if ($post_count) {
        echo \'<li>\';
        echo \'<a href="\'.get_bloginfo(\'url\').\'/author/\' . $user->user_nicename . \'">\'.get_avatar($user->user_email, \'36\').\'</a>\';
        echo \'<a href="\'.get_bloginfo(\'url\').\'/author/\' . $user->user_nicename . \'">\'.$user->display_name.\' (\'.$post_count.\')</a><li>\';
            }
          }
        }
尽管存在两个问题,但这是可行的:

我找不到一种方法让他们按帖子的数量进行排序。get\\u users\\u of\\u blog是一个被贬低的功能。我真的不想用它我能够用wp\\u list\\u作者创建一个分解,但不知道如何从中提取数据以这种方式显示。帮助

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

回答您的问题:

按帖子数量排序您可以将所需的所有信息收集到一个数组中,并按帖子数量对该数组进行排序,如下所示:

    //function to sort array by filed
    function authors_orderBy($data, $field){
       $code = "if (\\$a[\'$field\'] == \\$b[\'$field\']) {return 0;} return (\\$a[\'$field\'] < \\$b[\'$field\']) ? 1 : -1;";
       usort($data, create_function(\'$a,$b\', $code));
       return $data;
    }
然后更改代码,如下所示:

        $blogusers = get_users_of_blog();
        if ($blogusers) {
            $au = array();
            foreach ($blogusers as $bloguser) {
               $user = get_userdata($bloguser->user_id);
               $post_count = count_user_posts($user->ID);
               $au[] = array(\'user_id\' => $user->ID , \'nicename\' => $user->user_nicename, \'display_name\' => $user->display_name, \'email\' => $user->user_email ,\'post_count\' => $post_count);
            }

            //Sort array
            $au = authors_orderBy($au, \'post_count\');

           //then loop through the authors
           foreach ($au as $aut){
             if ($aut[\'post_count\'] > 0) {
                echo \'<li>\';
                echo \'<a href="\'.get_bloginfo(\'url\').\'/author/\' . $aut[\'nicename\'] . \'">\'.get_avatar($aut[\'email\'], \'36\').\'</a>\';
                echo \'<a href="\'.get_bloginfo(\'url\').\'/author/\' . $aut[\'nicename\'] . \'">\'.$aut[\'display_name\'] .\' (\'.$aut[\'post_count\'].\')</a><li>\';
             }
           }
        }
get\\u users\\u of\\u blog是一个贬值的博客,这很奇怪,因为看看法典是的,这个功能是贬值的,你应该使用get_users() 应随版本3.1一起提供

     /**
     * Retrieve list of users matching criteria.
     *
     * @since 3.1.0
     * @uses $wpdb
     * @uses WP_User_Query See for default arguments and information.
     *
     * @param array $args
     * @return array List of users.
     */
    function get_users( $args ) {

            $args = wp_parse_args( $args );
            $args[\'count_total\'] = false;

            $user_search = new WP_User_Query($args);

            return (array) $user_search->get_results();
    }
但如果您查看wp\\u list\\u authors,它会使用get\\u users\\u of\\u blog()本身。

希望这有帮助。

SO网友:Rarst

get_users_of_blog() 每个文档可能都不赞成,但实际上wp_list_authors() 内部使用(source ) 目前似乎并没有可行的替代方案。

我认为没有简单的定制方法wp_list_authors() 你想要的方式。只能建议在您自己的函数中重用它的代码,添加gravatar的修改将是最小的。

SO网友:Fask

code widget for sidebar:

add_action( \'widgets_init\', \'latest_members_widgets\' );

function latest_members_widgets() {
register_widget( \'Latest_Members\' );
}
class Latest_Members extends WP_Widget {
function Latest_Members() {
    $widget_ops = array( \'classname\' => \'latest_members\', \'description\' =>   \'Visualizza gli avatar degli utenti iscritti.\' );
    $this->WP_Widget( \'latest-members-widget\', \'Iscritti Recenti\', $widget_ops);
}


 function widget( $args, $instance ) {
    extract( $args );

    $title = apply_filters(\'widget_title\', $instance[\'title\'] );
    $avatars = $instance[\'avatars\']; ?>

    <?php echo $before_widget; ?>

    <?php if ( $title ) echo $before_title . $title . $after_title; ?>

    <div class="widget_latest_members">
    <ul>
        <?php global $wpdb;
        $szSort = "user_registered";
        $aUsersID = $wpdb->get_col( $wpdb->prepare(
        "SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY ID DESC LIMIT $avatars", $szSort ));
        foreach ( $aUsersID as $iUserID ) :
        $user = get_userdata( $iUserID );
        echo \'<a href="\'.get_bloginfo(url).\'/?author=\'.$iUserID.\'" rel="profilo" title="Vai al Profilo">\'.get_avatar($iUserID, 35,$default=$author->ID).\'</a>\';
        endforeach; ?>
        <br />
  <div class="totale"><?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users." utenti registrati";?></div>
</ul>
    </div>

    <?php echo $after_widget; }

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
    $instance[\'avatars\'] = strip_tags( $new_instance[\'avatars\'] );

    return $instance;
}

function form( $instance ) {

    /* Set up some default widget settings. */
    $defaults = array( \'title\' => \'Latest Members\', \'avatars\' => 20);
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>

    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>">Titolo:</label>
        <br/><input type="text" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'avatars\' ); ?>">Numero Avatar da visualizzare:</label>
        <br/><input type="text" id="<?php echo $this->get_field_id( \'avatars\' ); ?>" name="<?php echo $this->get_field_name( \'avatars\' ); ?>" value="<?php echo $instance[\'avatars\']; ?>" />
    </p>

<?php
}
    }

    ?>
结束

相关推荐

如何从wp_list_Authors中排除特定作者

我想让作者像往常一样从wp_list_authors() 但我知道有几个我也想从名单中排除。有办法吗?谢谢